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

David Luo
David Luo
1,215 Points

My code works on my IDE but I keep getting "Bummer!" error here

Perhaps I'm not understanding the question correctly? I don't know why this doesn't pass the test. (Code attached)

disemvowel.py
def disemvowel(word):
    li = list(word)
    for c in li:
        if c in ("aeiouAEIOU"):
            li.remove(c)
    return "".join(li)

1 Answer

andren
andren
28,558 Points

The problem is that you are removing items from a list while looping over that same list. Doing that tends to lead to unpredictable buggy behavior and is therefore considered a bad practice.

While your code works fine on some content it doesn't on others. It fails mostly in cases where a vowel appears repeatedly, for example if you pass the string "aaaa" to your function it will return "aa" which is clearly not correct.

This can be fixed by simply storing the list version in a variable and using the string itself for the loop source, like this:

def disemvowel(word):
    li = list(word)
    for c in word:
        if c in ("aeiouAEIOU"):
            li.remove(c)
    return "".join(li)
David Luo
David Luo
1,215 Points

Ahhhh, perfect. Thanks for the explanation -- such a silly "mistake" which I wasn't able to debug, but now understand exactly why it did what it did. Thanks!