8. anti_vowel last o not removed


#1

:confused: Trying this but it returns Hy lk words!. Anyone know why the last o is not removed? Canโ€™t work it out :confused:

 def anti_vowel(text):
  lst = []
  for letter in text:
    lst.append(letter)

  for char in lst:
    for vowel in "aeiouAEIOU":
      if vowel == char:
        lst.remove(vowel)
  return "".join(lst)

#2

When using .remove() the list collapses to fill the void created by removing an element. Since the pointer is moved one element to the right, it misses the element that just slipped into the previous position. That would be the second o in look.

.remove() takes the first target object it encounters, so when the o in word is reached, it removes the second o left over from look.

lst = list(text)

for char in text:
    if char in "aeiouAEIOU":
        lst.remove(char)

Above we iterate over text and remove items from lst. This way there is no loss of string length and the entire string is iterated.


#3

understood, thank you!


#4

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