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

Nicolas Montoya
Nicolas Montoya
10,592 Points

I do not think there is something wrong with my code

For some reason if there are two vowels in a row, it will not delete the second vowel. I can't see what is wrong with my code. Please help.

disemvowel.py
def disemvowel(word):

    # put all letters in word into a list
    # make a list of vowels
    # if letters are in list of vowels then I want to take them out
    # make a string out of the remaining letters
    vowels = "aeiouAEIOU"
    letters = list(word)
    for i in letters:
        if i in vowels:
            letters.remove(i)

    str1 = ''.join(letters)

    return str1

2 Answers

Steven Parker
Steven Parker
231,248 Points

This problem results from modifying an iterator inside the loop that it is controlling. Removing an item causes the loop to skip over the next item.

One way to avoid this is by using a copy of the item to control the loop.

Nicolas Montoya
Nicolas Montoya
10,592 Points

Thank you! I see that now.

Ben Reynolds
Ben Reynolds
35,170 Points

I would approach this by only having your list of vowels include lower or uppercase instead of both, then in your loop do either i.lower() or i.upper() when checking for a match. That way it doesn't matter if the word you pass in has caps or not.

Nicolas Montoya
Nicolas Montoya
10,592 Points

that makes sense. Thank you for the advice!