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

Pau Mayorga Delgado
Pau Mayorga Delgado
1,174 Points

Challenge Task 1

Hi I seem to get stuck all the time with this task and i'd require a bit of help. In this task I have to make a function (disemvowel) by which I can remove all the vowels from an input word. When I test it and I write "hello", it returns "hll" as it's supposed to. The problem is that when I try "how are you today" it returns "hw r yo today". So it just remove each vowel once. The second problem is when I try to use vowel.upper() and vowel.lower() for upper- and lowercase respectively, then I just get error. Any ideas how to move forward?

Thanks!

disemvowel.py
def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    word_list = list(word)
    for vowel in vowels:
        try:
            p = word_list.index(vowel)
            del(word_list[p])

        except ValueError:
            pass
        word = "".join(word_list)
    return word
new_word = input("> ")
vowelless = disemvowel(new_word)
print(vowelless)

1 Answer

For every vowel, you are only removing one instance of that vowel. This...

p = word_list.index(vowel)
del(word_list[p])

...only removes one vowel. You could repeatedly remove every vowel one by one, but then your code would be messy and be hard to debug. I recommend you write something like this:

def disemvowel(word):
    new_word = ''
    for letter in word:
        if letter.lower() not in 'aeiou':
            new_word += letter
    return new_word

If you want to be more elaborate and fancy, I'd recommend this:

def disemvowel(word):
    return ''.join([x for x in word if x.lower() not in 'aeiou'])
Pau Mayorga Delgado
Pau Mayorga Delgado
1,174 Points

Thank you for your quick answer, that was really clever (it will probably require some time for me to get there by my own).