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

Joel Webster
Joel Webster
1,303 Points

What's wrong with my disemvowel() function?

Seems to work fine in PyCharm but the challenge says it's getting back more letters than it should be?

disemvowel.py
def disemvowel(word):

    # convert word to list
    word_list = list(word)
    for char in word_list:
        if char.lower() in list("aeiou"):
            word_list.remove(char)

    # convert word_list back to string
    word = ''.join(word_list)

    return word

1 Answer

Hi Joel

Don't use the remove function, the reason being, it only removes the first occurrence in the list. See below i amended you code slightly, I reset word to en empty string, appended the letter that is not a vowel to the string word. Also, you dont need to convert the string aeiou to a list.

def disemvowel(word):

    # convert word to list
    word_list = list(word)
    # set word back to an empty string
    word = ""
    for char in word_list:
        if char.lower() in "aeiou":
            # if the letter is a vovel then pass
            pass
        else:
            word+=char

    # convert word back as a string
    return word

Thank you a lot, I have been working on this problem for days. I created one that worked on the Python interpreter but for some reason, it didn't work on Treehouse.

Muchas gracias,