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

David Chia
David Chia
6,988 Points

Why is my code not working as expected?

I tested my code with "adsfQWERuiOp" But the result was 'dsfQWRip'

Why is the 'i' still in the word?

disemvowel.py
def disemvowel(word):
    ls = list(word)
    for alpha in ls:
        if alpha.lower() in "aeiou":
            ls.remove(alpha)
    word = ''.join(ls)
    return word

1 Answer

Ryan S
Ryan S
27,276 Points

Hi David,

The issue of skipping vowels arises when you modify the same list that you are iterating through. Any time you have two vowels in a row, the second one will be skipped over because the indexes in the list will shift once you remove the first vowel.

One way around this is to iterate through a copy of the list, but still remove vowels from the original. Recall that a quick way to make a copy is by using slices (eg., ls[:]).

Hope this helps.