Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Lists Disemvowel

remove list item

i am not able to remove word from list

disemvowel.py
def disemvowel(word):
    L = ["a", "e", "i", "o","u"]
    for N in L:
        av = list(word.lower())
        for M in av:
            if M == N:
                av.remove(M)
    world = str(av)    
    return word

1 Answer

Steven Parker
Steven Parker
221,297 Points

Never remove items inside a loop from the iterable that is controlling the loop. This can case side-effects such as items being skipped over. Instead, you could iterate using a copy of the iterable (easily made with a slice), or you could create a new result in the loop and return that.

Also, don't convert the entire word to lower case. This will potentially modify some of the letters that you intend to keep (they should remain unchanged).

I don't think you can use "str" to convert a list into a string, but you can do it with "join".

And check your variable names. The code creates "world" but returns "word" (which is the original argument unchanged).

Thank u