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

Jason Christy
Jason Christy
1,105 Points

Python, disemvowel challenge - What am I missing?

The challenge: "The function disemvowel takes a single word as a parameter and then returns that word at the end. I need you to make it so, inside of the function, all of the vowels ("a", "e", "i", "o", and "u") are removed from the word. Solve this however you want, it's totally up to you! Oh, be sure to look for both uppercase and lowercase vowels!"

What am I missing here. I can get this to work in workspaces (using print instead of return so that I can see what it does) but in the challenge it fails and says that it's getting letters it didn't expect.

disemvowel.py
def disemvowel(word):
    vowels = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u']
    word_list = list(word)
    for letter in word_list:
        if letter in vowels:
            word_list.remove(letter)
    return "".join(word_list)

2 Answers

Bapi Roy
Bapi Roy
14,237 Points

With reference to @Rayan study the below code

def disemvowel(word): vowels = ['a','e','i','o','u', 'A','E','I','O','U']

ret_word = []
for i in word :
    if i not in vowels:
        ret_word.append(i)


return ''.join(ret_word)
Jason Christy
Jason Christy
1,105 Points

So.. create an empty list, then throw every letter that's not a vowel into that list then return that as a string.. Nice. Thanks Bapi, that is a great way of doing that that I wouldn't have thought of.

Ryan S
Ryan S
27,276 Points

Hey Jason,

Nice work, you pretty much got it. The one issue is that you are modifying word_list as you are iterating through it. As you remove vowels, you shift the indexes and can end up skipping letters. If you have 2 vowels in a row, for example, you'll find that it won't remove the second vowel.

To get around this, you could create a copy of word_list, and iterate through that while still modifying the original.

Good luck.

Jason Christy
Jason Christy
1,105 Points

Aaaah.. I didn't think about that. That totally makes sense. Thanks for pointing that out!