Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: