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

Joshua Bowden
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joshua Bowden
Full Stack JavaScript Techdegree Graduate 29,312 Points

Removing the vowels from a word function:

My code should be removing all of the letters that don't fit but for some reason is failing specifically on the u's which is weird.

disemvowel.py
def disemvowel(word):
    letters = []
    letters.extend(word)
    for letter in letters:
        if letter == 'a' or letter == 'A' or letter == 'e' or letter == 'E' or letter == 'i' or letter == 'I' or letter == 'o' or letter == 'O' or letter == 'u' or letter == 'U':
            letters.remove(letter)
    word = "".join(letters)
    return word

1 Answer

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

Hi there, Joshua Bowden ! The problem here is that you're changing the thing you're iterating over which is essentially messing up the indexing. What this means is that whenever you remove a letter, the check on the next letter will be skipped. That's not a problem when the next letter is a consonant, but it is a problem when the next letter is a vowel.

The way to mitigate this would be to first create a copy of that list. You can either iterate over the original list and change the copy, or iterate over the copy and change the original, but the one you're iterating over should not be changed. Then at the end, take the changed version and rejoin it to be a string and return it.

Hope this helps! :sparkles: