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

Removing the vowels not working!?

when i run this script in another editor is seems to work to perfection removing the Upper and Lowercase vowels. Why isn't it been successful here?

disemvowel.py
def disemvowel(word):
    test1 = list()
    test1.extend(word)
    try:
        for i in test1:
            if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
                test1.remove(i)
    except ValueError:
        pass

    try:
        for j in test1:
            if j == 'A' or j == 'E' or j == 'I' or j == 'O' or j == 'U':
                test1.remove(j)
    except ValueError:
        pass
    s = ""
    text_good = s.join(test1)
    word = text_good
    return word

2 Answers

loop through word not through list of word.

for i in word:
    # code
for j in word:
    # code

The request is that that the function take a single word:S. I tried the for i in word, still no avail

your code with my changes

def disemvowel(word): # take a single argument in this case is word
    test1 = list()
    test1.extend(word)
    try:
        for i in word: # lop through word 
            if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
                test1.remove(i)
    except ValueError:
        pass

    try:
        for j in word: # lop through word
            if j == 'A' or j == 'E' or j == 'I' or j == 'O' or j == 'U':
                test1.remove(j)
    except ValueError:
        pass
    s = ""
    text_good = s.join(test1)
    word = text_good
    return word

When you loop through a list, and remove an index, the lop is going like this:

word = "aabbccee"
# list(word) -> ["a", "a", "b", "b", "c", "c" ,"e", "e"]
# list(word) -> [ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ] -> index values
# after first lop list(word) looks like that
# [ "a", "b", "b", "c", "c" ,"e", "e"] ->item from index 0 is remove, the loop continues from index 1
# [ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ] -> index values
# after the loop is ready the list is like this
# ["a", "b", "b", "c", "c" ,"e"] -> Not all vowels were removed

I hope you understood. :)

Ahh now i get it! Thank you for you insight sir! Very helpful, been going 2 days at it.