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

Joshua Howard
Joshua Howard
12,676 Points

Unable to get ‘disemvowel’ function to work.

Maybe i’m having a slow day. Maybe i’m just not thinking correctly about this problem. But i’m not sure why this is not working. Is it because I can’t use the join function on a string that i’ve already used the split function on?

disemvowel.py
def disemvowel(word):

    vowels = ['A','a','E','e','I','i','O','o','U','u']
    letters = word.split()

    for letter in letters:
        if letter in vowels:
            letters.remove(letter)



    return ''.join(letters)

3 Answers

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

Hi there! Nope, that's not the reason. I'd suggest trying this in a workspace and send in the string "Treehouse". You'll get back some interesting results. This is because you're mutating the thing you're iterating over. Every time something is removed the check for the next letter will be skipped entirely.

Hope this hint helps! :sparkles:

Joshua Howard
Joshua Howard
12,676 Points

Thanks guys, I fixed it by using ‘list’ instead of split. Afterwards, looped through each iterative and appended the non-vowel characters into a new list.

Then I joined the list and returned that result.

Joshua Howard
Joshua Howard
12,676 Points

In the future, what would be a good way to go about getting it to continue looping until all vowels are removed without setting the length?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

For performance, a single linear pass-through is the best you can hope for. This can be done by iterating over a copy of letters so that any removals from the original do not affect the loop progression. There is a python idiom using slices you'll learn later in the course. Both of these work equally well:

# using built in copy method
for letter in letters.copy():
# using slice notation
for letter in letters[:]: