8. anti_vowel


#1

so my code returns "Hy lk Words!"
why is it not removing the last o?

def anti_vowel(text):
    vowels = "aeiouAEIOU"
    no_vowel = []
    for i in text:
        no_vowel.append(i)
    for i in no_vowel:
        for x in vowels:
            if i == x:
                no_vowel.remove(i)
    return "".join(no_vowel)
print anti_vowel("Hey look Words!")

#2

How does removing an element affect the iteration through that same list?


#3

I was adding the full text to the empty list, then I removed the vowels


#4

Yes, and as you remove them, how does that affect your loop?


#5

theoretically, it's supposed to remove the vowels but for some reason it didn't remove the third o


#6

Does your loop reach the third o? Something to do with removals affecting the loop perhaps?


#7

That makes sense. After removing the letter, the loop is moving to the next coordinate, not the next letter


#8

should I restart the loop every time I remove a letter?


#9

I suggest creating a new list, filling it up with just what you want to keep. That also ends up being faster, removing is pretty expensive since it requires all following elements to scoot up one notch to cover the empty slot.


#10

Ohhhhhhhhh ok thanks.


#11

Hope this may be of interest. Came up with this code snippet which seemed much simpler using python's List Comprehension

def anti_vowel(text):
vowels={'a','e','i','o','u','A','E','I','O','U'}

vtext=''.join(letter for letter in text if letter not in vowels)
return vtext

print anti_vowel("Hey look Words!")