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

Prem Yadav
Prem Yadav
7,346 Points

not able to solve this problem......

hey there In this program i have created a list for word and used try and except for vowel removing. but still not able to solve. Not getting where i'm doing mistake. please help to find out solution.

disemvowel.py
def disemvowel(word):
    for letter in list(word):
        try:
            letter.remove('a') or
            letter.remove('e') or
            letter.remove('i') or
            letter.remove('o') or
            letter.remove('u')
        except ValueError:
            pass
        else:    
        return word
Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points

Hey Prem,

I think the approach of this is that you need to take smaller steps.

  1. See if you can remove an element in your string and return it.
  2. Use your for loop and try to filter out a single vowel i.e. if letter == 'a': word.remove('a')
  3. Then try to use the 'x' in 'list' boolean operator to filter out against a list of vowels

Some issues I noticed:

  • You are using the remove method incorrectly. Remove works against a list, but you are trying to call remove on a string.
  • The use of else doesn't make sense, if and else usually go hand in hand, for exception handling you can use try, except, and finally
  • Note that tabs dictate the scope and loop for python, so if it managed to hit return, you would have only checked the first letter of a word.
  • When you cast 'word' into a list, to read it, it doesn't change the object like you think it would as you are just 'getting' the enumeration from the cast, it's not actually turning the object into a list, so either you will need to pass in a list initially, or create a variable with the 'word' casted as a list and use that.

Hope this helps!

Matt

1 Answer

Sneha Nagpaul
Sneha Nagpaul
10,124 Points

Hey Prem, This is how I would do it.

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

    for vowel in vowels:
        some_string = some_string.replace(vowel,"")

    return some_string