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

Savannah Logan
Savannah Logan
974 Points

disemvowel

I really dont see why this isnt working. I got it to work nicely in workspace

disemvowel.py
def disemvowel(word):
    word_list = list(word)
    for letter in word_list:
        if letter.lower() in 'aeiou':
            word_list.remove(letter)
    word = "".join(word_list)
    return word

3 Answers

Abdishakur Hassan
Abdishakur Hassan
3,585 Points

There is only one thing wrong with your code. You need to loop with the original word not the word_list. So your code will look like this:

def disemvowel(word):
    word_list = list(word)
    for letter in word:
        if letter.lower() in 'aeiou':
            word_list.remove(letter)
    word = "".join(word_list)
    return word
Savannah Logan
Savannah Logan
974 Points

Thank you. That did work, but why? It looks the same in workspace when I was testing... what is it doing differently?

seong lee
seong lee
4,503 Points

Yeah, why did it work, I want to know too?

Daniel Schmidt
Daniel Schmidt
9,780 Points

Try this:

def disemvowel(word):
    vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
    return ''.join([letter for letter in word if letter not in vowels])

If your not familiar with this syntax check out this course Python Comprehensions

In your code you need to change

for letter in word_list:

to

for letter in word:

because your code would just remove the lower case letters.