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 Disemvowel

Disemvowel

It seems that I always skip a letter. if you take the word "aououai" . It will remove one letter and does not remove the next one. However I do not understand why it would skip as the loops seems to be okay.

disemvowel.py
def disemvowel(word):
    word = list(word)
    vowels = list("aouie")

    for letter in word:
        for vowel in vowels:
            if(letter == vowel or letter == vowel.upper()):
                word.remove(letter)
    print(word)
    return word

4 Answers

Ryan S
Ryan S
27,276 Points

Hey Axel,

The issue you are experiencing with the skipping of letters is due to removing items from a list as you are looping through it. The problem is that when you remove a vowel, all of the indexes shift because that item is no longer there. So if you have a vowel at index 2, lets say, the loop knows to go to index 3 next, but since everything shifted, you will actually be looking at the letter that used to be at index 4.

To deal with this, you can loop through a copy of the list, but still remove vowels from the original. Slices are a quick way of making a copy, eg. for letter in word[:]:

Hope this helps.

Hi Ryan, Thank you for your answer! I get the point now why a letter was skipped. Didn't know they were using indexes for it at that point. Will read up some more docs about slices to really get it in my mind ;-)

Hi Ryan, Seems I am still getting an error that we got letters back that we weren't expecting. However when I am trying it here in Pycharm and my input is disemvowel("AOIUeuoiuopsirjhgouoreEI") I get the expected result.

The function is:

def disemvowel(word):
    word = list(word)
    vowels = list("aouie")

    for letter in word[:]:
        for vowel in vowels:
            if(letter == vowel or letter == vowel.upper()):
                word.remove(letter)
    print(word)
    return word

disemvowel("AOIUeuoiuopsirjhgouoreEI")
Ryan S
Ryan S
27,276 Points

Hey Axel,

Your logic is good but you are missing just one little thing. Don't forget that at the beginning of the function you turned the string into a list in order to modify it. You will need to turn it back into string before you return it.

Hi Ryan, Thanks was missing word = ''.join(word) I was just returning letters instead of the word.