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

Adam Tyler
Adam Tyler
14,865 Points

Disemvowel Challenge

Am I using the for loop incorrectly? I used this script in workspaces and ran it with : "disemvowel('cheese PLEASE')" This returned: "chse PLAS"

Not sure why it works on some of the vowels but not all, Thank you in advanced.

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

1 Answer

Hi Adam,

This is because you're deleting items from the array but the loop will carry on to the 'next' element. (And there's no lower case i in your list!) :wink:

So, you remove element 4, the loop moves on to element 5 but this is now what used to be element 6 as you deleted the 4th.

Make sense?

You're better approaching this by building a list of non-vowels, rather than deleting.

Steve.

Take the word TEAL. The elements are:

[0] T
[1] E
[2] A
[3] L

After the second pass, you delete [1] and the loop continues onto [2] but the remaining string is now:

[0] T
[1] A
[2] L

So you skip testing the A because you moved over it by deleting the element.

Best leave everything in place and build a new string of non-vowels.

Adam Tyler
Adam Tyler
14,865 Points

Thank you, I have completed the challenge now!

Good work! :+1: :smile: