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

Brian Haucke
Brian Haucke
13,717 Points

Python disemvowel challenge. My code is not accepted, although it works in other text editors.

The disemvowel challenge will not accept this code, but I've tried it in other text editors and it appears to work just fine. I keep getting the message "Bummer! Hmm, got back some letters I wasn't expecting!" Can someone tell me why this isn't working in the challenge? Thanks.

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

    return word

2 Answers

Manish Giri
Manish Giri
16,266 Points

In Python, it's not advisable to mutate a list while you're traversing through it. Which is what you're doing in your code.

I amended your code slightly. This code checks that if the current letter in the word is not a vowel, it appends this letter to a new list. Then it concatenates and returns the list as a string -

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    word_list = []
    for letter in word:
        if letter.lower() not in vowels:
            word_list.append(letter)
    word = "".join(word_list)
    return word
Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

It looks like your code doesn't preserve case of the original word. I've made the challenge show more on why it is failing,

Let me know if that does the trick for ya!

Brian Haucke
Brian Haucke
13,717 Points

Thanks Craig, that helped quite a bit! I added capital letters to my vowels list and tinkered around a little more and got it to work!