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

Kevin S
seal-mask
.a{fill-rule:evenodd;}techdegree
Kevin S
Data Analysis Techdegree Student 15,862 Points

Why isn't my code working here.

When i run the function, it takes out most of the vowels but seems to arbitrarily leave one in the string of characters. Can someone figure out why?

Thanks!

disemvowel.py
def disemvowel(word):
    word = list(word)
    for letter in word:
        seached = letter.lower()
        if seached == 'a':
            word.remove(letter)
        elif seached == 'e':
            word.remove(letter)
        elif seached == 'i':
            word.remove(letter)
        elif seached == 'o':
            word.remove(letter)
        elif seached == 'u':
            word.remove(letter)
        else:
            continue
    return ''.join(word)

1 Answer

Strange things can happen when you modify the iterable you are looping on within the loop. Instead, try making a new variable which holds a string, and if the current letter is not a vowel, append the letter to the variable:

def disemvowel(word):
    new_word = ''
    for letter in word:
        if letter.lower() not in 'aeiou':
            new_word += letter
    return new_word

I hope this helps! :grin: :zap: ~Alex

EDITED