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

Nick Lenzi
Nick Lenzi
7,979 Points

disemvowel challenge: My code works perfectly yet the challenge won't take it..?

It works perfectly when I run it an IDLE. Even ran it in a debugger to ensure the return value is proper but for some reason the challenge submit won't accept it saying "hmm, got back letters I wasn't expecting?" Am I missing something obvious or is the challenge being picky again. I mean i don't see why it should because it says I can solve it however I want sooooo...

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

3 Answers

Stuart Wright
Stuart Wright
41,120 Points

Removing elements from a list as you iterate over it causes issues. Have you tested your code by passing various different words to it? How about one which has two vowels in a row?

A better way would be to create a new blank list and append non-vowels to it as you iterate over the original word.

Nick Lenzi
Nick Lenzi
7,979 Points

jeeeeez i didn't even think to test repeat vowels thank youuuu. I'm curious what you mean by removing elements from a list while iterating over it causing issues though. Could you maybe go a little more in depth on this?

Stuart Wright
Stuart Wright
41,120 Points

If you imagine looping over the word 'four', this is what your original program would have done:

  • Check the first element in the list. It's not a vowel so do nothing.
  • Check the second element in the list. It's a vowel so remove it. 'o' gets removed, and 'u' becomes the second element.
  • Check the third element in the list. Whoops! The 'u' was never checked.
Steven Parker
Steven Parker
231,269 Points

It may only work "perfectly" with some words.

Try a word like "bookkeeper". :smirk:

There's a problem with using something as the basis for iteration and then altering it inside the loop. That can cause some items to be skipped over. Some ways to avoid that would be to use a copy of the item for iterating, or to build a new item to be returned. The first method is probably easiest and can be done using a slice:

    for letter in word[:]:
Nick Lenzi
Nick Lenzi
7,979 Points

ok that is definitely useful knowledge I probably would not have learned without you. Thanks! I love breaking down the bare bones of how a process works.