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

Stuck on challenge 1 of Python Collections

Hi, I get the error:

Hmm, got back letters I wasn't expecting! Called disemvowel('LPsOboDai') and expected 'LPsbD'. Got back 'LPsbDi'

I'm not sure why the "i" at the end is the only vowel that still sneaks in there.

Thanks.

disemvowel.py
def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    letters = list(word)
    for letter in letters:
        if letter.lower() in vowels:
            letters.remove(letter)
    return ''.join(letters)

1 Answer

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

Hi there, Kate Dougherty! You're doing great and this is a recurring theme. Many people notice that a vowel doesn't get removed here and there. I know it seems random, but there is a pattern. Because you are mutating the thing you are iterating over, the indexing gets messed up. The result is that any time a letter is removed, the check on the following letter will be skipped entirely. This isn't a problem if the next letter is a consonant, but when you have two vowels in a row, this presents a problem. If we have the word "tree", the first "e" will be removed, but the check for the *second" e will be skipped.

To mitigate this you can make a copy of the original list. Once you do this, you can either iterate over the original and mutate the copy or iterate over the copy and mutate the original. After that's done, you should join the mutated form as you did here and return it.

Hope this helps! :sparkles:

Thanks Jennifer, that worked! Would be great if either the training material or the code challenge could be edited to address this issue though. I don't know how I would have had any idea how to deal with this without your assistance.

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

Kate Dougherty I'm glad I could help :smiley: The problem here is that there are multiple ways to solve this challenge. There are even solutions that don't require a list at all. The best way to have done troubleshooting on this was to either run it in a workspace or on your local system and see if you got back what you expected. This would likely have required some print statements to see what is being checked and what is being removed.