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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Why doesn't my version of disemvowel pass?

So the function is supposed to remove all the vowels. I fumbled through using replace and finding that it doesn't get every instance, then looked around online and found two solutions that are supposed to remove every instance of every vowel, and I 've run them through my jetbrains IDE but the code challenge rejects them as "returning vowels". I am out of ideas. Any hints?

thanks.

disemvowel.py
def disemvowel(word):
    l = list(word)
    l[:] = (value for value in l if 'a' != value)
    l[:] = (value for value in l if 'e' != value)
    l[:] = (value for value in l if 'i' != value)
    l[:] = (value for value in l if 'o' != value)
    l[:] = (value for value in l if 'u' != value)
    word = "".join(l)
    return word

7 Answers

Steven Parker
Steven Parker
231,128 Points

:point_right: Your vowel identification is case-sensitive.

While this solution seems rather unusual and complex, it's conceptually sound. But this particular implementation only removes vowels that are lower-case.

You should be able to pass the challenge if you make your comparisons case insensitive, or if you include upper-case vowels in the set of characters you remove.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

I got it.

def disemvowel(word):

   l = list(word)
    vowel = ['a', 'e', 'i', 'o', 'u','A','E','I','O','U']
    for item in l:
        if item in vowel:
            l.remove(item)

    word = "".join(l)
    return word

Hi Nancy,

Good job figuring it out.

Modifying a list while you're iterating over it can lead to problems, especially when you're removing items.

This code isn't going to work out if you have 2 consecutive vowels. It will miss the 2nd vowel.

You can try your code locally with "team" to see what I mean. It becomes "tam"

Instead, you can iterate over a copy of the list while you remove items from the original list. You can use slice notation to obtain a copy.

for item in l[:]:
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Like Steven Parker said, what about uppercase vowels?

Also, this seems really overengineered. Is there a simpler way?

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

I will keep working on it (both problems - the upper case vowels and the overengineering.)

Thanks!

this doesn't pass for me as well :

def disemvowel(word):
    vowels = ['a','e','i','o','u']
    return ''.join([i for i in word.lower() if i not in vowels])
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Why are you lowercasing the word? Do the instructions say to do that?

Because the code challenge said that all vowels has to be removed, whether upper or lower case. And that's why I made that mistake, I think I figured it out, thanks.