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

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