Anti vowel


#1

def anti_vowel(text):
l=list(text)
print l
for n in l:
if n=='a' or n=='A' or n=='e' or n=='E' or n=='i' or n=='I' or n=='o' or n=='o' or n=='u' or n=='U':
l.remove(n)
print l
text= ''.join(l)
print text
return text
anti_vowel("Hey, look words ")

This removes some 'o's but keeps some. im confused, any ideas??


#2

Okay note this from now on removing elements from a list while iterating it is a bad idea
that being said you best bet is to create a new list then append only the desired characters to it and then finally you join them all together.
Or you can use list comprehension


#3

I tried the method you suggested and it worked, but i still wonder if there is a logical fault in the method i tried earlier.


#4

To see what he was talking about, do this

numbers = [1, 2, 3, 4, 5]
for i in numbers:
    numbers.remove(i)
    print(i)

That should give you a idea of what's going on.

Also I would change your vowel code to

word = 'string'
for letter in word.lower():
    if letter in 'aeiou':
        print(letter)

You can use the fact that a string can be iterated over with an if statement to your benefit this time. It will allow you to simply declare what you want to find.