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

David Pešava
David Pešava
1,212 Points

Problem with solving code challenge - Disemvowel function

Hi there, i have problem to cpmlete the challenge, i tried it in workspace, and there it works. Can you help me please with reviewing my code?

disemvowel.py
def disemvowel(word):
    vowels = ["a", "e", "i", "o","u"]
    word = word.lower()
    word = list(word)
    for letter in word:
        if letter in vowels:
        word.remove(letter)   
    word = ''.join(word)
    return word
Dario Bahena
Dario Bahena
10,697 Points

It does not seem too clear to me in the explanation but it seems like the function should return the string passed to it in its original case. so "SomEsTrIng" should return "SmsTrng". Yours looks like it might return "smstrgn" instead.

# The solution I wrote was using a for loop and two while loops.
# The first for loop just loops five times, once per vowel
# The first while converts the vowel to upper case and then checks if it is in the word
# The remove method only removes the first occurrence of the item passed to it, that is why the while loop is there.
# The second while loop does the same as the first but for lower case. The vowel variable does not need to be turned lowercase since it is already declared as lowercase in the vowelList variable
def disemvowel(word):
    vowelList = ["a", "e", "i", "o","u"]
    word = list(word)
    for vowel in vowelList:
        while vowel.upper() in word:
            word.remove(vowel.upper())
        while vowel.lower() in word:
            word.remove(vowel)
    return "".join(word)

1 Answer

Hi David,

I approached this one by looping through the word parameter and inspecting each letter in turn. I used a for loop and called the local variable letter for the sake of clarity. At each iteration, I checked the lowercase version of letter. If it was not in a list of lowercase vowels, I added the letter to an empty string I initialised at the beginning of the method. I called that output. I return output after the loop has finished working.

So, the output is made up of all characters in word (in the right order) that aren't vowels. The case of the character isn't altered, as required by the challenge, but the lowercase version is used for the comparison.

That all looked like:

def disemvowel(word):
    output = ""
    for letter in word:
        if letter.lower() not in ['a', 'e', 'i', 'o', 'u']:
            output += letter
    return output

I hope that makes sense.

Steve.

Chris Reilly
Chris Reilly
12,086 Points

Great solution, thanks Steve!

Chris