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

Function in workspaces gives different response than in task

So my function works in workspaces, on my local computer and on my Raspberry server but whenever I use the function on the task it gives a different response

disemvowel.py
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
to_delete = []

def disemvowel(word):
    word = list(word)
    print(word)
    for index, i in enumerate(word):
        print(index, i)
        if i in vowels:
            to_delete.append(index)
            print(to_delete)
    for indexed in sorted(to_delete, reverse=True):
        del word[indexed]
    word = "".join(word)
    return word



print(disemvowel("PMrs"))

1 Answer

Managed to solve it myself with a more efficient method

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]

def disemvowel(word):
    word = list(word)
    for i in vowels:
        try:
            word.remove(i)
        except ValueError:
            continue
    word = "".join(word)
    return word