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

Tobias Edwards
Tobias Edwards
14,458 Points

My 'disemvowel' function isn't passing the challenge?

Question OK, I need you to finish writing a function for me. The function disemvowel takes a single word as a parameter and then returns that word at the end. I need you to make it so, inside of the function, all of the vowels ("a", "e", "i", "o", and "u") are removed from the word. Solve this however you want, it's totally up to you! Oh, be sure to look for both uppercase and lowercase vowels!

Anyone know why it isn't passing the challenge? I've tested the function and it seems to work fine.

Thanks

disemvowel.py
def disemvowel(word):
    word = list(word)
    for letter in word:
        if letter.lower() in ["a", "e", "i", "o", "u"]:
            word.remove(letter)
        else:
            continue

    return "".join(word)

1 Answer

Hi Tobias,

You'll get indexing issues if you use a for loop and remove elements from the iterable being looped over. For example, if you loop over the word STEVE the first iteration will be holding the S. If you then remove that, the second iteration will move to the now second element, i.e. the first E, it'll omit the T entirely.

1 2 3 4 5
S T E V E 
# first iteration holds S

Delete S in the first iteration:

1 2 3 4 
T E V E
# second iteration holds E

For this challenge, it is easiest to build up an output string of non-vowels by using not in, I found.

I created a blank string at the top of the method and appended to that any letter.lower() that was not in the list of vowels. Once the loop was completed, I returned that string.

Let me know how you get on.

Steve.

Tobias Edwards
Tobias Edwards
14,458 Points

Wow! I changed my code and it worked :) Thank you Steve for the clear and in-depth response

Glad you got it working! :+1: :smile: