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

Corinn Zieglgansberger
Corinn Zieglgansberger
2,419 Points

disemvowel issue-taking all the letters except the last one

Hi, when I run this code it is only returning the last letter of the string that was the input. I don't understand at all what's going on. Thanks for your help

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

1 Answer

Steven Parker
Steven Parker
229,644 Points

If you modify an iterable inside the loop it is controlling, it can throw off the internal indexing and cause items to be skipped over.

Try iterating with a copy of the iterable, so the loop won't be affected when an item is removed.

Corinn Zieglgansberger
Corinn Zieglgansberger
2,419 Points

It's only returning the last letter of the string no matter the length. So it's taking out consonants and vowels. Making a copy of it didn't solve the problem. Thanks though

Steven Parker
Steven Parker
229,644 Points

Sorry, I spotted a common issue and just led with that. Here's some hints for each issue:

  • remember to loop using a copy of an iterable if it will be modified in the loop
  • testing for membership in a list is done with the word "in" (instead of "==")
  • use an empty string ("") with "join" unless you want to add spaces between each item
  • "letter" is just the last letter tested, so you'll want to join using "test" instead

I'll bet you can get it now, but write again if you still have trouble!