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

Ali AlRabeah
Ali AlRabeah
909 Points

Remove vowel function. Why isn't my code working?

The logic is there it seems. Every time it returns the word I get back the same original string. Any help appreciated.

disemvowel.py
def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    for letter in word:
        if letter in vowels:
            del letter
    return word

2 Answers

Ines Fazlić
seal-mask
.a{fill-rule:evenodd;}techdegree
Ines Fazlić
Python Web Development Techdegree Student 9,569 Points

Hello Ali, the thing is that word is essentially a string and strings are immutable. So try creating another string that is the initial word without the vowels, and then return it.

Ali AlRabeah
Ali AlRabeah
909 Points

I've completed the challenge but in a different way. Every time I ran:

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

It would only remove some of the vowels it detected. I'm still not sure why. For example: disemvowel("UECwoe") Would return as "ECwe"

So I figured it needed to run through all the vowels so I put a for loop for the vowels.

def disemvowel(word):
    vowels = ['a', 'e', 'i', 'o', 'u']
    word_list = list(word)
    for vowel in vowels:    
        for letter in word_list:
            if letter.lower() in vowels:
                word_list.remove(letter)
    new_word = ''.join(word_list)
    return new_word

Do you know why the first one wouldn't work and the second one did? Also, could you elaborate on what you mean by creating another string? Where would it be placed?

Ines Fazlić
seal-mask
.a{fill-rule:evenodd;}techdegree
Ines Fazlić
Python Web Development Techdegree Student 9,569 Points

Hey, I just ment that you can't change the given string, by telling you to make another string I ment to do a list remove vowels and join it into a new string, but I just wanted to give you a hint and not the whole answer so you figure it out.

The first code would work if you had put: for letter in word_list.copy ():

we add the .copy() because when you modify the list while looping through it you can get weird results and it kinda misbehaves :) so we use a copy of the list and for every item from the copy we remove that item from the original list :)

I hope this helps

Ali AlRabeah
Ali AlRabeah
909 Points

I have to remember to use copies instead. Appreciate it.