"AJAX Basics (retiring)" was retired on February 5, 2020. You are now viewing the recommended replacement.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Removing Items From A List

Nasir Khalid
seal-mask
.a{fill-rule:evenodd;}techdegree
Nasir Khalid
Python Web Development Techdegree Student 4,779 Points

Having difficulty in the 'disemvowel' challenge.

Hello friends! I am having some trouble here. So I want to remove these vowels but my code is giving weird results. It goes through the loop and removes the first vowel then skips the next one then removes the third one and so on.This is not what I intended to do but I don't know how it keeps doing it :P Any help would be appreciated! Thanks! I know this is some redundant code so I will work on improving it but this is my first try at it.

def disemvowel(word):
    word_tolist=list(word)
    for letter in word_tolist:
        try:
            if letter.lower()=='a':
                word_tolist.remove('a')
            elif letter.lower()=='e':
                word_tolist.remove('e')
            elif letter.lower()=='i':
                word_tolist.remove('i')
            elif letter.lower()=='o':
                word_tolist.remove('o')
            elif letter.lower()=='u':
                word_tolist.remove('u')
        except ValueError:
            pass
    word=''.join(word_tolist)
    return word

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The problem here is that you're changing the thing you're iterating over and this is causing some checks to be skipped entirely. In the case where a check on a consonant is skipped, it doesn't matter, but obviously it matters when the check on a vowel is skipped. Essentially, the indexing of the iterable gets messed up. Let's take the word "Balloon" for example. Here's the order of events.

  • Checks B ... nothing happens
  • Checks a ... "a" is removed and "l" falls down into its spot so the check for the first "l" never happens
  • Checks the second "l"... nothing happens
  • Checks the first "o", it is removed and the second "o" falls down into its spot so the check for the second "o" never happens and it's left
  • Checks for the "n" and nothing happens.

At the end, the word returned will be "Bllon", but that isn't correct and it's because a check was skipped on a vowel. Every time you remove a letter, the very next letter will not be checked.

The way to fix this is to either iterate over a copy and mutate the original, or iterate over the original and mutate a copy.

Another moderator and I posted very detailed descriptions about what is happening with the indexing of the iterable when this is done. Take a look at this thread.

Hope this helps! :sparkles:

Nasir Khalid
seal-mask
.a{fill-rule:evenodd;}techdegree
Nasir Khalid
Python Web Development Techdegree Student 4,779 Points

Ohh right I get it now :P So basically when I remove a letter the next letter takes the index of the previous one is then essentially skipped from the loop. Right. Got it :) Thank you so much for clearing my confusion!