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

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.