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

removing items from a list

i need you to finish writing a function for me.The function disemvowel takes a single word as a parameter and then returns that word at the end. I need you to make it so, inside of the function, all of the vowels ("a", "e", "i", "o", "u")

def disemvowel(word): vowels = ["a", "e", "i", "o", "u"] vowels = [ element.lower() for element in vowels] new_list [] for letter in list(word): if not letter.lower()in vowels new_list += letter new_list =".join(new_list) return new_list

1 Answer

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi Marshall. You are very close This lines are ok

def disemvowel(word): 
    vowels = ["a", "e", "i", "o", "u"] 

you can also define vowels like so:

    vowels = 'aeiou'

This is a list to. The third line you don't need

vowels = [ element.lower() for element in vowels] 

After this when you define the "new_list", it's missing the "=" sign

new_list = []

In the for loop you don't need to convert to a list the incoming "word" variable

for letter in word:

Remember, string it's already a list. These 2 lines are ok to just it's missing the ":" from the "if" statement and a space before the "in" keyword

if not letter.lower() in vowels:
    new_list += letter

From here you can return the result directly

return "".join(new_list)

So overall it looks like this:

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    new_list = []
    for letter in word:
        if not letter.lower() in vowels:
            new_list += letter
    return ''.join(new_list)

Or a very short version

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    return ''.join(letter for letter in word if letter.lower() not in vowels)

I hope it will help you to figure out. Happy coding