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

ghaith omar
ghaith omar
16,395 Points

vowel challenge

Hi all I am trying to do the vowel challenge in python course. I wrote a code but didn't work in the challenge, but its working when I try it. The challenge about removing any vowel on the world. this is my code :

def disemvowel(word): x = "" word = list(word.lower()) while True: if "a" in word: word.remove("a") if "e" in word: word.remove("e") if "o" in word: word.remove("o") if "i" in word: word.remove("i") if "u" in word: word.remove("u")
if "a" in word or "o" in word or "e" in word or "i" in word or "u" in word: continue else: break

for gn in word:
            x +=gn
word = x
return word
disemvowel.py
def disemvowel(word):
    x = ""
    word = list(word.lower())
    while True:
        if "a" in word:
            word.remove("a")
        if "e" in word:
            word.remove("e")
        if "o" in word:
            word.remove("o")
        if "i" in word:
            word.remove("i")
        if "u" in word:
            word.remove("u")   
        if "a" in word or "o" in word or "e" in word or "i" in word or "u" in word:
            continue
        else:
            break

    for gn in word:
                x +=gn
    word = x
    return word

5 Answers

Ah - you're not preserved the case of the word sent in.

You can get round that by testing if letter.lower() not in vowel, rather than changing the whole word to lowercase.

So, lose word_lower completely and loop through word (not word_lower) using letter, as you have done. Then change your if statement to the above and that should work, I think.

ghaith omar
ghaith omar
16,395 Points

Thanks it worked, when I removed .lower() from word, and add it to letter.lower() thanks for help

Glad you got it sorted. :+1:

HI all ! I wrote this and tested it on repl.it. But the Teamtreehouse system doesn't accept it. What's wrong in here? Any one want to help?

def disemvowel(word):
    word = list(word)
    remove_this = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    no_vowels = []
    for i in word:
        if i in remove_this:
            word.remove(i)
        else:
            no_vowels.append(i)
    no_vowels = "".join(no_vowels)
    return (no_vowels)

If you remove from the thing you are iterating over, you cause real problems.

Let's iterate over my name. On the first pass, we're on 'S', which we then remove for whatever reason. We start with:

1 2 3 4 5 
S T E V E 

After removing 'S' we are left with:

1 2 3 4
T E V E 

But the loop will continue to iteration number 2, so you bypass the 'T' completely and go straight to 'E'.

That's why it's easier to build a string of the characters you want rather than remove the characters you don't want.

Make sense?

Steve.

You've actually done what you need to - there's more lines than required and there's one that causing the issue. If you just comment out your remove line and replace it with a pass your code works.

def disemvowel(word):
    word = list(word)
    remove_this = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    no_vowels = []
    for i in word:
        if i in remove_this:
            pass
            #word.remove(i)
        else:
            no_vowels.append(i)
    no_vowels = "".join(no_vowels)
    return (no_vowels)

However, you can be more brief:

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

Steve.

Hi there,

One issue is that you've changed the case of word so your returned string will not be the same as the input string, minus vowels.

Try looking at this a little differently. Let's create an empty string to output/return at the end of the method. Let's create a list holding all the vowels, too. Then, use a for loop to iterate over the parameter, word.

At each iteration, see if the lower case version of the current letter is not in the list of vowels. If it isn't, add it to the output string.

At the end of the loop - return that string.

Give that a try and let me know how you get on.

Steve.

ghaith omar
ghaith omar
16,395 Points

Thanks for the fast reply. I try this method and didn't work, this the new code

def disemvowel(word):

    x = ""
    vowel = ["a","o","i","u","e"]
    word_lower = list(word.lower())
    for letter in word_lower:
        if letter not in vowel:
            x += letter   
    return x

Looks good - let me see what's up with it ...

Ismail KOÇ
Ismail KOÇ
1,748 Points

If you want output words are upper and lower, you can use this code:

def disemvowel(word):
    x = ""
    word = list(word)
    ret_list = word                                            # add this for do not damage word list for remove vowels.
    vowels = ['a','e','i','o','u','A','E','I','O','U']
    for i in word:
        if i in vowels:
            ret_list.remove(i)
    for j in ret_list:                                         
        x += j                                                 # return string
    return x
print(disemvowel('abceFiGu'))

I added vowels = ['a','e','i','o','u','A','E','I','O','U'] because deleting lower and upper vowels and return of the function only deleting vowels. (this is alternative, you can use this if you want)

Or you can just test if i.lower() in vowels: which makes your list shorter too. :smile:

ghaith omar
ghaith omar
16,395 Points

Thanks for the answer.

Ismail KOÇ
Ismail KOÇ
1,748 Points

Or you can just test if i.lower() in vowels: which makes your list shorter too. :smile:

yeah you are right (how can i add other emojis in texts)

Wow steve thank you very much for taking time to answer my question :-) !

No problem! :+1: