Modifying an iterator (your list) while looping over it will end up yielding very unexpected results, and should always be avoided. In your particular case, as you delete your list, you are actually reducing it, and therefore actually ending it "earlier" than you should.
What you should be doing instead is collecting your data in a new list based on what you are trying to filter. So, instead of popping
, instead you should check for non-vowels and append to a new list. Taking your exact code and changing just that logic, you should be good:
def anti_vowel(text):
l = []
s = ""
for i in text:
l.append(i)
new_l = []
for i in l:
if i not in "aeiouAEIOU":
new_l.append(i)
print("".join(new_l))
So, running that code now, will yield the following output:
Hy lk wrds!
Now, to go over some areas in your code where you are doing some unnecessary steps that you can simplify. You do not need to loop through your string like that and create a new list at the beginning of your function. Your string can already be iterated over, so simply do exactly that using your string. In other words you do not need this:
l = []
s = ""
for i in text:
l.append(i)
So, instead, you can just start from:
new_l = []
for i in text:
if i not in "aeiouAEIOU":
new_l.append(i)
print("".join(new_l))
So, the above code now simply iterates over your string text
, character-by-character, and we will check to make sure that each character does not match the vowels, and append it to our new list.
Finally, for the sake of really making this short. We can throw this in to a pretty little line making an expression that we can then call join on to create our string. Also! you don't need to check all cases since you can just keep your characters to a single casing by calling the lower
method on your string you are iterating through to keep all under a single case, to make it simpler to check:
def anti_vowel(text):
return ''.join(c for c in text if c.lower() not in "aeiou")
print(anti_vowel("Hey look words!"))