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

Arun Patel
Arun Patel
1,180 Points

What's wrong with the letters being returned

What's wrong with the letters being returned

disemvowel.py
def disemvowel(word):
    word = list(word)
    word = [letter.lower() for letter in word]
    try:
        word.remove("a")
        word.remove("e")
        word.remove("i")
        word.remove("o")
        word.remove("u")
    except ValueError:
        pass
    word = ", ".join(word)
    return word
Ronald Lira
Ronald Lira
12,217 Points

Hey, a couple of comments.

  1. It is a good practice (and a good idea) to use a different variable for doing any changes on your input variables unless you really know what you are doing, otherwise, you lose your original data.

    word_lst = list(word)
    
  2. Also, at this point, you already have a list. If you want to lower case your input, do it before splitting your data.

    word_lst = list(word.lower())
    

    However, note that this will lowercase all the letters, not just the vowels.

  3. Then, you are trying to remove vowels in your try statement. However, the method remove() only remove the first occurrence of the argument from the calling array. In other words, if you have for example:

    >>> my_string = "aAeEiIoOuU123zXcVbNmaAeEiIoOuU"
    >>> my_list = list(my_string)
    >>> my_list
    ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', '1', '2', '3', 'z', 'X', 'c', 'V', 'b', 'N', 'm', 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U']
    >>> my_list.remove('a')
    >>> my_list
    ['A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', '1', '2', '3', 'z', 'X', 'c', 'V', 'b', 'N', 'm', 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U']
    

    As you can see only the first a was removed. How do you deal when you have the same vowel multiple times?

For the previous example, your final output should be:

>>> output
'123zXcVbNm'

Note that the letters keep their original case.

My solution:

Usually, people solve this using a regular expression, but you are probably not familiar with that yet. Also, I see you are trying to use python list comprehensions, which are really powerful, so I will keep the same style:

def disemvowel(word):
    VOWELS = 'AaEeIiOoUu'
    return "".join([letter for letter in word if letter not in VOWELS])

Or which is the same:

def disemvowel(word):
    VOWELS = 'AaEeIiOoUu'
    my_list  = []
    for letter in word:
        if letter not in VOWELS:
            my_list.append(letter)

    return "".join(my_list)

Hope this helps,

Ronald

2 Answers

Steven Parker
Steven Parker
231,007 Points

A few issues stand out at first glance:

  • the entire word is converted to lower case, but the letters not removed are expected to stay as they were
  • the list version of "remove" only removes the first item that matches
  • when a match for any letter is not found, no remaining letters will be removed
  • using ", " as the join string doesn't reconstruct the list into a word
Arun Patel
Arun Patel
1,180 Points

Thanks Steven. Thanks Ronaldo for the detailed explanation and clarification. This helped clarify my query.