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

Mikey Ro
Mikey Ro
6,948 Points

Can anyone help me refine my code?

The only way I could solve this was to use a new list to write the non-vowels to. But this would use additional memory - is there a way to better solve this problem?

def disemvowel(word): vowels = ['a','e','i','o','u','A','E','I','O','U'] new_word = [] for i,v in enumerate(word): try: if v not in vowels: new_word.append(v) except ValueError: continue

return ''.join(new_word)

1 Answer

Antonio De Rose
Antonio De Rose
20,885 Points
def disemvowel (text):
    vowel = ["a", "e", "i", "o", "u"]
    chars = []

    for letter in text:
        if letter.lower() not in vowel:
            chars.append(letter)

    return "".join(chars)

disemvowel('antonio')