Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,938 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
221,938 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!