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

Benjamin Guyton
Benjamin Guyton
6,858 Points

disemvowel

I've tried this a couple of different ways, such as the code attached, along with attempting to use a try/exempt approach, and though both ways seems that it would return the right word, it's not saying so. Any helpful hints here would be appreciated, I'm not sure why this code isn't a solution.

disemvowel.py
vowels = ["a", "e", "i", "o", "u"]
place = 0

def disemvowel(word):   
    while place < 5 :
        if vowels[place] in word.lowercase() :
            word.remove(place)
        else :
            place += 1

    return word

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Your thinking is correct, but Python strings don't have a remove method -- so instead, we use the replace method. And then the extra "gotcha" here is it has to work for both UPPER and lower case.

There are so many ways that this can be done. Some Python 'studs' may suggest a Lambda Function or a List Comprehension to make it a "one-liner." Ignore them and do it your way -- you are the one in charge! :-)

vowels = ["a", "e", "i", "o", "u"]

def disemvowel(word):
    # go through all the vowels
    for letter in vowels:
        word = word.replace(letter,'') # replace letter with empty string if lowercase vowel
        word = word.replace(letter.upper(),'') #replace with empty if uppercase vowel

    return word

Slightly more concise way below.

def disemvowel(word):
    vowels = 'AEIOUaeiou'
    for letter in vowels:
        word = word.replace(letter,'')
    return word