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

Luca Tardito
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Luca Tardito
Front End Web Development Techdegree Graduate 17,602 Points

hi guys, I don't understand where is the error in my code!

I don't know why when the word has 2 vowels nearly this function delete only the first one and leave the second one

disemvowel.py
vowels = ["a","e","i","o","u"]
def disemvowel(word):
    word1 = list(word)
    for letter in word1:
        if letter.lower() in vowels:
            word1.remove(letter)
    return word1

1 Answer

Hi there,

You're really close - just a couple things:

  • We want to return a string with the vowels removed, and word1 is a list - we need to convert it back into a string using .join() before we return it.

  • You generally don't want to remove values from the iterable that you are currently looping through - it can cause entries to be skipped, because it throws off the positioning of the contents. However, word1 was formed from word, and they'll have the same contents - strings are iterables too, so the solution is to loop through word, while removing values from word1.

These two things just require a couple small changes. It would look like this:

vowels = ["a","e","i","o","u"]
def disemvowel(word):
    word1 = list(word)
    for letter in word:  #iterate through word instead of word1
        if letter.lower() in vowels:
            word1.remove(letter)

    word1 = "".join(word1)  #convert word1 back into a string
    return word1