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

srikanth soma
srikanth soma
1,572 Points

I think I'm very close can someone help me where I'm doing wrong?

def disemvowel(word):
    vowels=['a','e','i','o','u','A','E','I','O','U']
    word = list(word)
    for letters in word:
        if letters in vowels:
            word.remove(letters)
    str = ''.join(word)    


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


    return str

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are very close! You've fallen into a classic error of modifying the object word that is under iteration. When you alter the iterable object by removing items the loop reference pointers end up skipping items in list.

The simple fix is to iterate over a copy of word so that letter removal from the actual word doesn't affect the loop.

for letters in word.copy():

# or use slice notation which you'll learn soon
for letters in word[:]:

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

srikanth soma
srikanth soma
1,572 Points

You mean like this?

def disemvowel(word):
    vowels=['a','e','i','o','u','A','E','I','O','U']
    word_copy = list(word)
    for letters in word_copy:
        if letters in vowels:
            word_copy.remove(letters)
    str = ''.join(word_copy)    


    return str
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

This is still removing characters from the object under iteration. Instead, try

def disemvowel(word):
    vowels=['a','e','i','o','u','A','E','I','O','U']
    word_copy = list(word)
    word = list(word)
    for letters in word_copy:
        if letters in vowels:
            word.remove(letters)
    str = ''.join(word)    


    return str

ProTip: using ```python in your formatting will colorize the code for Python!

srikanth soma
srikanth soma
1,572 Points

Why are you iterating over word_copy and deleting from a word? word_copy and word will have same contents so, why to make an additional copy word?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It's true word and word_copy contain the same contents, but they are not the same object and may be altered independently of each other. This is important to allow deleting from the working copy word while not affecting the iteration copy word_copy. Otherwise you'll be back in the modified iterable error case.