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

Jeremy Schaar
Jeremy Schaar
4,728 Points

Python disemvowel challenge

This is for the "disemvowel" challenge in Python collections. Kenneth asks us to make a function called disemvowel that takes a single word as a parameter and then returns that word at the end with no vowels (lower or uppercase).

I figured out a way to solve it! But along the way I tried the following code:

def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    word = list(word.lower())
    for letter in word:
        if letter in vowels:
            word.remove(letter)
        elif ValueError:
            continue
    word = ''.join(word)
    return word

That did some (to my mind) strange things. Here are some words an what would get returned... abebibobu -> bbbb AbeIAsEssa -> bisss My name is Jeremy --> my nm s jrmy aAeEiIoOuU -> aeiou

So... it's doing something along the right lines, but clearly wrong. My question is what is actually happening with my script? Why is it taking out some vowels and not others?

2 Answers

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

Hi there, Jeremy! You are not the first person to ask this question and undoubtedly will not be the last. The problem lies in that you are altering the iterable that you're looping over. This is causing some checks to not occur at all because the indexes of the iterable are constantly changing. I left an answer and a very length example of what is actually happening when you do this on another thread. Note: you will have to scroll all the way down to see the example I left. Feel free to run it in workspaces!

You can view the thread at this link

Hope this helps! :sparkles:

Jeremy Schaar
Jeremy Schaar
4,728 Points

Awesome explanation! Thanks!