Anti-vowel attempting to use remove


#1

Can someone please explain why my code doesn’t run correctly? Sorry I am very new to this

def anti_vowel(text):
  lst = []
  new = ""
  for char in text:
    lst.append(char)
 
  for item in lst:
    if item == "a" or item == "e" or item == "i" or item == 'o' or item == "u" or item == "A" or item == "E" or item == "I" or item == "O" or item == "U":
      lst.remove(item)
      
  for item1 in lst:
    new = new + item1
  return new
anti_vowel("Hey Look Words")

Why does anti_vowel fail in some cases?
#2

The result in the console is “Hy Lk Words”


#3

index shift, lists can’t have empty indexes so lets say we have the following:

l   o   o   k
0   1   2   3

i put the indexes under it.

so then we loop over look, and then the o at index 1 gets removed. Now the list can’t have that empty spaces, so everything to the right of the removed item gets moved one spot to the left:

l   o   k
0   1   2

then the loop continues to index 2, causing successive vowels to be skipped. Which is why remove is not a good approach to use.


#4

Your explanation is superb.
Thank you and so sorry for the dumb question.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.