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

Juan Antonio Mejia Vazquez
Juan Antonio Mejia Vazquez
903 Points

disemvowel

I don't know what is wrong with this code, could someone help me out?

disemvowel.py
def disemvowel(word):
    vowels = ['a','e','i','o','u']
    word = list(word)
    for letter in word:
        if letter in vowels:
            word.remove(letter)
    word = format("".join(word))
    print("Your word without vowels is {}".format(word))
    return word

2 Answers

Hi Juan,

Your code isn't going to work anytime you have consecutive vowels. You'll always miss the second vowel.

This is because you're modifying word while you're iterating over it. When you remove one vowel, all the remaining letters move down one spot to occupy that empty spot. So you end up missing whichever letter came after the one you just removed.

Instead, you can iterate over a copy of word while you remove letters from word

 for letter in word[:]:

I used slice notation to get a copy.

Juan Antonio Mejia Vazquez
Juan Antonio Mejia Vazquez
903 Points

Thanks Jason, now the script works by removing all the vowels from the word, I don't quite undestand how the slice notation works yet, but I'm experimenting with it. Something weird is that I'm still not able to pass the code challenge, not even with the solution you gave me. I tested it in PyCharm and works.

I'm not sure what your code looks like at this point but you can compare with the following:

def disemvowel(word):
    vowels = 'aeiouAEIOU'

    word = list(word)
    for letter in word[:]:
        if letter in vowels:
            word.remove(letter)
    word = format("".join(word))
    print("Your word without vowels is {}".format(word))
    return word

This is mostly your code with the 2 necessary corrections. Looping over a copy of the word and accounting for uppercase vowels. As Torsten mentioned, the format() function isn't doing anything here but it will pass with it. I would take that out as well as the print statement. Extra things like that can sometimes cause your code not to pass.

Sorry about the suggestion to use slice notation. I didn't realize it wasn't covered until the next stage. It should make much more sense after covering the next stage.

I'll give you some hints on an alternative solution that I think is easier to understand and I think it only uses what you should know up to this point.

Here's some commented code. Each comment can be replaced with 1 line of code.

def disemvowel(word):
    # initialize a vowels string to contain all the lower and uppercase vowels
    # initialize a variable to be an empty string. Letters that are not vowels will be added to this string
    # loop over each letter in word
        # if the letter is not a vowel
            # add the letter to the string using string concatenation
    # return the string

The idea behind this one is that you build up a new string consisting of all the letters in word that are not vowels.

Juan Antonio Mejia Vazquez
Juan Antonio Mejia Vazquez
903 Points

Thanks Jason, that hint really helped. I finally passed the code challenge

Torsten Lundahl
Torsten Lundahl
2,570 Points
    word = format("".join(word))

Theres is no need for the format(). Simply remove it and assign the join method directly to word.

    word = "".join(word)
Torsten Lundahl
Torsten Lundahl
2,570 Points

And also! Your function isn't looking for uppercase vowels. Add uppercase vowels to the list to make sure you are removing those too.

vowels = ['a','e','i','o','u','A','E','I','O','U']
Juan Antonio Mejia Vazquez
Juan Antonio Mejia Vazquez
903 Points

Torsten, I made the changes you mention but still there are some words that don't work like "antonio", it leaves: ntno. I used pycharm to debug the script but somehow it skips the last vowel.