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

what is wrong with my code?

def disemvowel(word): word_list = list(word) for letter in word_list: if letter in ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]: word_list.remove(letter) word_string = ''.join(word_list) return word_string

disemvowel.py
def disemvowel(word):
    word_list = list(word)
    for letter in word_list:
        if letter in ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]:
            word_list.remove(letter)
    word_string = ''.join(word_list)
    return word_string

3 Answers

Steven Parker
Steven Parker
231,269 Points

When you alter an iterable while it is being used to control a loop, it can cause items to be skipped over. One way to avoid this is to iterate using a copy of the iterable.

Or you could simply iterate on the original string word instead :wink:

Steven Parker
Steven Parker
231,269 Points

That would work in this case, but it's not a good practice in case the number or order of items got changed in the conversion process.

True.

Thank you Steven and Alexander for your answers! I tried using the original string word, but then I could not use the remove() method in the if statement.

Steven, Could you please explain how to use a copy of the iterable to solve this question?

Steven Parker
Steven Parker
231,269 Points
    for letter in word_list.copy():   # or...
    for letter in word_list[:]:       # an unlimited slice is also a copy

Thank you Steven for your help! Much appreciated!