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

unable to remove items from a list

my code is no deleting the vowels from a word as a list.

disemvowel.py
def disemvowel(word):
    #change the word to a mutable data type
    word_list = list(word)
    vowels = ['a','e','i','o','u','A','E','I','O','U']
    while vowels in word_list:
        for each_vowel in vowels:
            word_list.remove(each)
    return "".join(word_list)

2 Answers

Steven Parker
Steven Parker
229,980 Points

If you remove elements from an iterable while it is controlling a loop, it can cause other elements to be skipped over. You could use a copy of the list for iteration, but an even easeir fix would be to use the original word as the loop iterable while you work on the list.

And the "while" list is testing to see if the vowels list is a member of the word list, that's probably not what you intended there.

Also, you reference "each" in the "remove" method, but it was not defined. Did you mean "each_vowel"?

Trevor J
seal-mask
.a{fill-rule:evenodd;}techdegree
Trevor J
Python Web Development Techdegree Student 2,107 Points
while vowels in word_list: # This returns False so it goes stright to return
     for each_vowel in vowels:
        word_list.remove(each_vowel)
return "".join(word_list)

 # I would try doing the while loop inside the for loop.
 for x in y:
     while x in list:
        list.remove(x)
 return "".join(word_list)