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

Juan Davin
Juan Davin
2,792 Points

Disemvowel

This code runs well in workspaces but I cant seem to get it correct

disemvowel.py
def disemvowel(word):
    new=list(word)
    for l in new:
        if l.lower()=='a':
            new.remove(l)
        elif  l.lower()=='e':
            new.remove(l)
        elif l.lower()=='i' :
            new.remove(l)
        elif l.lower()=='o':
            new.remove(l)
        elif l.lower()=='u':
            new.remove(l)
        else:
            break
    so=''.join(new)
    return so

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are modifying a list while looping through it. when you call remove, it removes the indicated element, then shifts remaining elements to the left. the loop moves on to the next element however, which means elements will get skipped. to fix you can make a copy and loop through that while modifying the original, or vice versa. when this code is run on a word with multiple consecutive vowels, some vowels slip through, because they are being skipped over.

Juan Davin
Juan Davin
2,792 Points

ohh i see thank you

I know this isn't really answering your question, but to simplify your code a bit you could make a list of vowels like vowels = ['a', 'e', 'i', 'o', 'u']. Then you could check if letter in vowels in order to check if the letter is a vowel. This may make it a little easier for you to debug your code.