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

shubhamkt
shubhamkt
11,675 Points

disemvowel

I am trying to remove the vowels one by one but it is not working. Any suggestions

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Unfortunately, this approach will not work as it doesn't take into account words with multiple instances of the same vowel. For example, the word "balloon". If I were to send that in, it would only remove one instance of "o" and what would be returned would be "bllon", which still contains one vowel. So I'm going to give some pseudocode here that hopefully will help you understand the the approach I used.

create a list that contains all vowels that need to *not* be in the returned string
create a new empty string
loop through the word
   if the letter is not in the vowels list
      append it to the new empty string
return the new string with all letters that are not in the vowels list

Hope this helps! :sparkles:

shubhamkt
shubhamkt
11,675 Points
def disemvowel(word):
    vowel=["a", "e", "i", "o", "u","A","E","I","O","U"]
    word=list(word)
    string=[]
    for letter in word:
        if letter  not in vowel:
            string.append(letter)

    return string

Moderator edited: Added markdown so the code renders properly in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

shubhamkumartrivedi There are a couple of ways to do this. In the code you posted you are turning word into a list and then appending the letter onto an empty array. The result will be that string will contain an array of letters instead of a string of characters. In this case you will need to use this line before you return string.

string = ''.join(string)

Alternatively, you could skip that all together and do something slightly shorter. This was my approach:

def disemvowel(word):
    vowel=["a", "e", "i", "o", "u","A","E","I","O","U"]. 
    string = ""  #declared an empty string
    for letter in word:
        if letter  not in vowel:
            string += letter. #add the letter to the string
    return string  #return the string

In the second approach there is no need to turn the word into a list nor join the elements of a list to form a string. Rather we start with a string and keep it that way through the duration.

Hope this helps! :sparkles: