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

nakalkucing
nakalkucing
12,964 Points

My disemvowel function won't pass the challenge.

The Check in the challenge will send me a message saying that it gave my code a random assortment of letters ie: "oigO" and received back: "gO" and it's not always uppercases or after a consonant. It can be a vowel of either case at any random point in the assortment. However, when I try it in Workspaces it works without a flaw. If anyone can show me my problem I would be grateful. Thanks

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

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

It's usually not a good idea to edit something as you iterate over it. It can result in items being skipped. For an example of a word that fails when passed into your function, try "treehouse".

A better way to complete this challenge would be to create a new list, append non-vowels to that, then return those letters joined to form a string.

List.remove modifies the list in place, meaning in the following iteration, the list is not the same list anymore. Ex: Word = 'Treehouse' On the third iteration, letter 'e' will be removed and the worl list would become 'Trehouse'. On the forth iteration, for loop comes to the fourth element and finds 'h' not 'e' because remove method changed your word to trehouse. That's why letter 'e' will not be looked at.

nakalkucing
nakalkucing
12,964 Points

Hi. Thank you both for your time. I tested 'Treehouse' in my function and it failed. Like you said. Thanks. I wrote the below code to try to see where my original code was going wrong.

word = "Treehouse"

def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

    split = list(word)

    print('split')
    print(split)

    for letter in split:

        print('letter')
        print(letter)

        if letter in vowels:
            split.remove(letter)

    print('split')
    print(split)

    word = "".join(split)

    print(word)

    return word

disemvowel(word)

This might be what you were saying, but I think my problem is before I even start removing letters. I think my problem comes before my printing 'Treehouse' as individual letters. Thanks again for your time.