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 (Retired) Lists Redux Disemvoweled

My version, anyone want to critique it?

It works, but my code always seems to be longer/more complicated than it needs to be...

# Treehouse Challenge number 2
#   - Write a script that takes for a word (or list of words)
#     removes all of the vowels, and gives the word (or words)
#     back.

word = "Treehouse"
print(word)


def remove_vowels(aWord):
    vowels = list('aeiou')
    alist = list(aWord)
    for vowel in vowels:
        while True:
            try:
                alist.index(vowel)
            except:
                break
            index = alist.index(vowel)
            del alist[index]
    print(alist) 

remove_vowels(word)

1 Answer

Ryan Merritt
Ryan Merritt
5,789 Points

Comprehensions are a lot of fun, and can definitely be used to shorten this code. Maybe something like...

def remove_vowels(my_word):
    vowels = list('aeiou')
    voweless_word = [letter for letter in my_word if letter not in vowels]
    return voweless_word 

I like how conversational the comprehensions can be! :D

Edit: There's a workshop on TeamTreehouse all about comprehensions, and python's docs also has great info.

Wow, I haven't heard of comprehensions before now, it really does trim it down significantly! It's weird how intuitive Python code is, I didn't even have to look at the Python.org doc to get a good idea of what the code does, even though I've never seen this done before. Thanks for the response!