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

I do not understand why my code is not correct?

When I try this code in my workspace it works correctly, however i the codechallenge it shows that "got back letters, that did not expect"...

Any ideas?

disemvowel.py
def disemvowel(word):
    lista = list(word)
    try:
        while lista.count('a'.lower()) > 0:
            lista.remove('a'.lower())
    except ValueError:
        pass
    try:
        while lista.count('e'.lower()) > 0:
            lista.remove('e'.lower())
    except ValueError:
        pass
    try:
        while lista.count('i'.lower()) > 0:
            lista.remove('i'.lower())
    except ValueError:
        pass
    try:
        while lista.count('o'.lower()) > 0:
            lista.remove('o'.lower())
    except ValueError:
        pass
    try:
        while lista.count('u'.lower()) > 0:
            lista.remove('u'.lower())
    except ValueError:
        pass 
    try:
        while lista.count('a'.upper()) > 0:
            lista.remove('a'.upper())
    except ValueError:
        pass
    try:
        while lista.count('e'.upper()) > 0:
            lista.remove('e'.upper())
    except ValueError:
        pass
    try:
        while lista.count('i'.upper()) > 0:
            lista.remove('i'.upper())
    except ValueError:
        pass
    try:
        while lista.count('o'.upper()) > 0:
            lista.remove('o'.upper())
    except ValueError:
        pass
    try:
        while lista.count('u'.upper()) > 0:
            lista.remove('u'.upper())
    except ValueError:
        pass 
    word = ''.join(lista)

    return word

2 Answers

Hi, this is not programming style. Python code should be short and pretty.

def disemvowel(word):
    vowels = 'aeiouAEIOU'
    word_list = list(word)

    for letter in reversed(word_list):
        if letter in vowels:
            word_list.remove(letter)

    return ''.join(word_list)
``

Thank you!!!!

Tom Fardell
Tom Fardell
4,676 Points

Why is word_list reversed?

You can't iterate over the array in which you are going to remove items. (iterate from start and delete) Imagine it as different array. You are going to iterate over all letters, so it doesn't matter in which order. But, in my opinion it is better iterate from behind and delete from the same array. Especially if you have classic for-loop available, also you can use while-loop.