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

I am stuck on Challenge 1 of Python Collections.

Hello fellow Treehousers,

I am stuck on Challenge 1 of Python Collections.

The Challenge's instructions are as follows:

"OK, I need you to finish writing a function for me. The function disemvowel takes a single word as a parameter and then returns that word at the end. I need you to make it so, inside of the function, all of the vowels ("a", "e", "i", "o", and "u") are removed from the word. Solve this however you want, it's totally up to you!"

My code is here.

  def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    for letter in word:
        if letter in vowels:
            word.remove(letter)
    return word

Thanks a bunch in advance for the help!

15 Answers

Umesh Ravji
Umesh Ravji
42,386 Points

https://teamtreehouse.com/community/stage:11592

The other ideas from other topics:

  • Using string concatenation to build a new string each iteration of the loop.
  • Converting the input string to a list, then using remove. Then converting back to string.
  • Creating an empty list, then appending characters to it each iteration of the loop. Then converting back to string.

You just cannot use remove on a string :)

Thanks Umesh! I will try and use these methods.

** Updated **

Here is the method I found to work. Didn't try the other ones, but am happy to have heard multiple solutions for the problem thanks to Umesh.

word = "pizza"
blank_word = ""
vowels = ["a", "e", "i", "o", "u"]

for letter in word:
  if letter not in vowels:
    blank_word += letter
  print(blank_word)
Dave Laffan
Dave Laffan
4,604 Points

If it helps, I used

def disemvowel(word):
    vowels = "aeiouAEIOU"
    new_word = []

    for letter in word:
        if letter not in vowels:
            new_word.append(letter)

    new_word = ''.join(new_word)
    return new_word

which passed. However, why can't I use

for letter in word.lower():

and restrict the vowel string to just lowercase vowels? It works in PyCharm, but not for this challenge?

Umesh Ravji
Umesh Ravji
42,386 Points

I'm going to assume some of the tests involve uppercase characters, which are ending up lowercase in the resulting string you are returning.

You can use lower() while returning original lower/upper. Via solution below:

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

Why you can't use just lowercase vowels?

Say your word is AlphaBeta if you use word.lower, The result after the execution will be:

lphbt

But the actual result must be:

lphBt

M Khan
M Khan
7,024 Points

That's brilliant

Dave Laffan
Dave Laffan
4,604 Points

Sorry Umesh, don't quite understand what you are saying? If uppercase chars are involved, the test will pass so long as the vowels string contains both upper and lower to check against. However, in my efforts to make the code better, I replaced the for.... line with the single line of code above. I figured, by converting all to lower before any checks, this would negate the need to check for upper.

In PyCharm, this worked, and allowed me to replace the vowels string with just the lowercase vowels, but this wasn't accepted by the Treehouse challenge?

Mubarig CISMAN
seal-mask
.a{fill-rule:evenodd;}techdegree
Mubarig CISMAN
Python Web Development Techdegree Student 2,547 Points

Hi Benjamin, If you are still stuck in the Challenge, This should work for you.

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    vowels = [element.lower() for element in vowels]
    new_list = []
    for letter in list(word):
        if not letter.lower() in vowels:
            new_list += letter
    new_list = ''.join(new_list)
    return new_list
Thomas Beaudry
Thomas Beaudry
29,084 Points

Nicely done Mubarig CISMAN, it took me awhilw to get this one!

RUIBO LIU
RUIBO LIU
8,051 Points
vowels = [element.lower() for element in vowels]

What's this line actually doing?

Hello, Thank you so much for this I just have a few questions. for letter in list(word): >>for this part why did you use list(word) not just word if not letter.lower() in vowels:>> I don't quite understand what this statement means. I think it means if the lower case of the letter is not a vowel added it to the new empty list you made before. Is that correct? Thank you for this. I got a better understanding of the exercise but I want to make sure I understand every part of the code as I found this challenge to be very difficult for me.

Manuela Sabatino
Manuela Sabatino
9,515 Points

That is my answer, I hope it can help you out:

def disemvowel(word):
    vowels =  ("a", "e", "i", "o","u", "A", "E", "I", "O", "U")
    for vowel in word:
        if vowel in vowels:          
            try:
                word = word.replace(vowel, "")
            except ValueError:
                pass
    return word
Daniel Escobar
Daniel Escobar
2,580 Points

Manuela Sabatino, why parenthesis when you are creating a list? I tried the same code but with square brackets and my code does not work:

def disemvowel(word): vowels = ['a', 'e', 'i', 'o', 'u'] for letter in word: if letter in vowels: try: word = word.replace(letter, '' ) except ValueError: pass print(word)

disemvowel("Carlos")

this is my code. Any help would be appreciated

Sean Flanagan
Sean Flanagan
33,235 Points

Thank you Manuela Sabatino.

Let me see if I understand correctly.

vowels =  ("a", "e", "i", "o","u", "A", "E", "I", "O", "U")

So this line lists all the vowels, in lower case then upper case?

The for loop creates a new variable called vowel and in that loop is an if statement. If there's a vowel in the word, go to the try block. that vowel will be replaced by an empty string in the word? In the case of a valueError, pass, which means nothing happens?

Umesh Ravji
Umesh Ravji
42,386 Points

Strings are not mutable in python, so you cannot remove from them. May I ask how the challenge is bugged? It appears to be passable as long as one includes capitals and lowercase vowels.

Thanks for the comment Umesh! It is bugged. What would be the correct way to write the code? Is it possible just by using remove?

Peter Javorkai
Peter Javorkai
31,477 Points

I'm a newbie to Python, and I'm also having some issues solving this challenge. Do you have a correct example to solve it?

Hi Peter. If you post your code I can definitely take a look at it and see where you're getting caught up.

Peter Javorkai
Peter Javorkai
31,477 Points

Thanks, for the quick answer Samuel Piecz my code is the following:

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u"]
    word = letters[]
        try:
            letters[] - ([vowels].upper() + [vowels])
        else:
            pass
    ", ".join(letters) = word
    return word

Hi Peter. My greatest apologies for such a late response. I got sidetracked.. Here is how I ended up solving the challenge. Best of luck!

vowels = ["a", "e", "i", "o", "u"]
word = "pizza"
empty_string = ""
for letter in word:
    if letter not in vowels:
        empty_string += letter

print(empty_string)
Peter Javorkai
Peter Javorkai
31,477 Points

Thanks Samuel, for some reason it's not accepting for me, but I'll keep trying! :/

Hi Peter. You can't copy and paste it if that is what you're doing. It must be reformatted for the function that the challenge asks for. The answer should help ;)

Peter Javorkai
Peter Javorkai
31,477 Points

Thank you Dave Laffan and Umesh Ravji for the help, it's amazing how active this community is. I'm amazed and better understood the issue.

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Dave Laffan, while the code does work in PyCharm, it doesn't produce the same result. For example: disemvowel('BANANA') results in BNN. Where as, if you use word.lower(), your result becomes bnn (which is not the correct answer.

Dave Laffan
Dave Laffan
4,604 Points

Ah yes, thank you. Simple, when you put it like that :smile:

Mubarig CISMAN
seal-mask
.a{fill-rule:evenodd;}techdegree
Mubarig CISMAN
Python Web Development Techdegree Student 2,547 Points

In order to lower case the vowels, we can go through the list in one go

   vowels = ["a", "e", "i", "o", "u"]
   vowels = [element.lower() for element in vowels]
Tucker Weston
PLUS
Tucker Weston
Courses Plus Student 13,128 Points

def disemvowel(word): vowels = ['a','e','i','o','u']

string_vowels = ''.join(vowels)
upper_vowels = list(string_vowels.upper())
vowels += upper_vowels

word_list = list(word)

for vowel in vowels:
    for letter in word_list:
        if vowel == letter:
            word_list.remove(letter)
word = ''.join(word_list)

return word
BENJAMIN UWIMANA
PLUS
BENJAMIN UWIMANA
Courses Plus Student 22,636 Points

def disemvowel2(word): vowels = ["a","e","i","o","u"] my_list = list(word) word = "" for letter in my_list: if letter.lower() not in vowels: word += letter print(word) return word

disemvowel2("BEnjamin")