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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Got back the letters I wasn't expecting

I ran thesame code in workspaces and it gives the right output. All vowels are removed. Any Idea why it gives the error?

disemvowel.py
def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    new = list(word.lower())
    for letter in new:
        if letter in vowels:
            new.remove(letter)
            word = new
    return word
    print(word)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Not quite. Try sending this word into your code: "balloon". What you'll get back is ['b', 'a', 'l', 'l', 'o', 'n']. The problem happens when you have two vowels consecutively. The code removed the vowel it's looking at and removes it from that index. Then it continues to the next index, but the previous index is now occupied by the vowel it should be removing. Every time you remove something, you're changing the length of the list.

Hope this helps, but let us know if you're still stuck! :sparkles:

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Thanks Jennifer. I modified the code to consider the index shifting, the baloon now works in workspaces and other words also like tree. But I still get the same error when I submit the work in the current challenge. See the code below:

def disemvowel(word):

vowels = ['a', 'e', 'i', 'o', 'u']

new = list(word.lower())

final_output = list(word.lower())

for letter in new:

    if letter in vowels:

        final_output.remove(letter)


        word = final_output
return word

Hi Abdulkadir,

You're lower casing the entire word that comes in which means you're lower casing the non-vowels also. The returned string isn't maintaining the original casing of the consonants.

Instead, you can lower case the letter you're comparing in your if conditional. That way nothing is getting permanently lower cased.

Probably the easiest thing to do is to add the upper case vowels to your vowels list and don't do any lower casing at all.

Also, vowels can be a string which makes it easier to type out.

vowels = "aeiouAEIOU"