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

Help eliminating vowels from words

Hi, I am trying to write a function that iterates through a given word and removes any vowels. Please let me know if you see anything I'm doing wrong! Thanks!

disemvowel.py
def disemvowel(word):
    word_list = list(word)

    index = 1
    for letter in word_list:
        if letter.lower() == "a" or letter.lower() == "e" or letter.lower() == "i" or letter.lower() == "o" or letter.lower() == "u":
            word_list.remove(letter)
        index +=1

    return word

1 Answer

Benjamin Lange
Benjamin Lange
16,178 Points

Hi there,

Your method looks great! You properly removed the vowels from word_list. There is one minor issue that is causing your method to return the incorrect answer: you neglected to update the word variable with the result. Set the word variable equal to the remaining characters in word_list using the join method as demonstrated in the below code snippet.

word = ''.join(word_list)

Let me know if you have any further questions.

Quick edit: You can remove the index variable from your code. Python for loops do not typically require the use of an index variable.

Thank you for response! My code is running now but I'm running into an error where if there are consecutive vowels it will miss the second one. For example: Monsoon will return Mnson

Benjamin Lange
Benjamin Lange
16,178 Points

Hi there! It looks like I missed another detail! What's happening here:

  • The for loop is using an index in the background, automatically.
  • When you remove an item from the list, the index for the next item in the list and all following items is reduced by one.
  • The next item in the list is skipped because you made the list shorter and the index automatically increased.

Thankfully, this is an easy fix.

  • Make a copy of your word_list (make sure that you use the list's copy method to create a new copy).
  • Instead of using the original list in the for loop, use the copy.
  • Remove the letters from the original list.