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

malfred koryor
malfred koryor
2,093 Points

disemvowel problem

can some please help me out, can't seem to figure out this one. Thanks!

disemvowel.py
def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O","U"]
    for letter in vowels:
        if letter.upper() and letter.lower() in list(word):
            word.remove(letter)


    return word

2 Answers

Charles Kenney
Charles Kenney
15,604 Points

Malfred,

You're close but there are two problems. The first one being that remove is not a string method, you should be using the replace method. The second one is, you need to update the value of the word. This was my solution:

def disemvowel(word):
    vowels = "AEIOUaeiou"
    for char in word:
        if char in vowels:
          word = word.replace(char, "")
    return word

Hope this helps,

Charles

Derek Gella
Derek Gella
15,215 Points

what's interesting is that the instructor didn't refer to the replace method in the lessons leading up to this challenge. Your version is probably the most simple in terms of code... but I would have never guessed to use the methods you had.

Charles Kenney
Charles Kenney
15,604 Points

Derek,

My apologies. I didn't know that the remove method was not introduced that early in the course. I actually opted to learn python from books and didn't really complete the treehouse course. I based all of the context of this problem on the instructions.

If you're curious however, the most elegant way to solve this problem would be using regular expressions and anonymous (lambda) functions. It'd look like this:

import re

disemvowel = lambda s: re.sub(r'[aeiou]', '', s)

If you like, you can read about these exciting topics here and here.

Hope you enjoy learning python!

Derek Gella
Derek Gella
15,215 Points

Charles, thank you for the references. Let me clarify; I believe the instructor introduced the replace method previously but the specific method was not a major point of focus that I am aware of leading up to the challenge. That is why I was a little awestruck with your solution. Regardless, it is clear that you are a talented programmer. Keep up the good work and thank you for your contributions.

Charles Kenney
Charles Kenney
15,604 Points

You're welcome! If only recruiters felt that way, haha. Have a good night!