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

within a for loop, using remove method to remove values from a list, then afterwards, each element in the list is added

within a for loop, using remove method to remove values from a list, then afterwards, each element in the list is added to a new string. why isnt this working:?

disemvowel.py
def disemvowel(word):
    lstWord=[]
    newWord=''
    lstWord.extend(word)
    for letter in lstWord:
        if letter=='a' or letter=='A' or letter=='e' or letter=='E' or letter=='i' or letter=='I' or letter=='o' or letter=='O' or letter=='u' or letter=='U':
            lstWord.remove(letter)
    for letter in lstWord:
        newWord = newWord + letter
    word=newWord
    return word

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The error is in the code:

    for letter in lstWord:
        if letter=='a' or letter=='A' or letter=='e' or letter=='E' or letter=='i' or letter=='I' or letter=='o' or letter=='O' or letter=='u' or letter=='U':
            lstWord.remove(letter)

You are modifying the iterable of the for. This throws off the indexes in the for loop. Instead, iterate on a copy of listWord

 for letter in lstWord.copy():

Also, the following code can be replaced:

# replace this
    for letter in lstWord:
        newWord = newWord + letter
# with
    newWord = "".join(lstWord)

Post back if you need more help. Good luck!!!