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

My function works...but is not accepted.

I have tested this outside this python environment and it works.

disemvowel.py
def disemvowel(word):
    vowels = ["a", "A", "e", "E", "i", "I", "o", "O", "u", "U"]
    word_list = list(word)
    for vowel in vowels:
        try:
            word_list.remove(vowel)
        except ValueError:
            pass
    word = ''.join(word_list)
    return word

2 Answers

Hi there,

I believe the issue is that, since you're iterating through the vowel list, it will only remove the first occurrence of each vowel. I tried your code out to confirm, and I believe that's why it is failing the challenge.

***Modified to reflect discussion below, as an oversight was pointed out

There are always multiple ways to solve something, but one thing you could try that wouldn't differ too much from your current code is iterating through the word instead, and removing the corresponding value from the word_list if it is a vowel. That way, it would still catch recurring vowels and make sure you test each letter in the word.

Hope this helps!

andren
andren
28,558 Points

one thing you could try that wouldn't differ too much from your current code is iterating through the word_list instead, and removing the value if it appears in vowels

Actually that would be a bad idea, as he would then be removing items from the list while iterating over it, that causes the items of the list to move around while the loop is running which causes it to skip over items.

Directly modifying the source of a loop inside said loop is considered a pretty bad practice.

A better idea is to iterate over the word variable, that contains the same letters but won't be changed as he removes things from the word_list.

You're right, that was an oversight on my part. Thanks for pointing it out - looks like that's what he ended up doing.

Thanks both of you. Took both of your guys' advice and was able to alter my function to pass the challenge.

def disemvowel(word):
    vowels = ["a", "A", "e", "E", "i", "I", "o", "O", "u", "U"]
    word_list = list(word)
    for letter in word:
        try:
            if letter in vowels:
                word_list.remove(letter)
        except ValueError:
            pass
    word = ''.join(word_list)
    return word