anti_vowel question


#1


https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2

When i run this code it doesn't remove the "o" in "Words" from the text, Why?
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


def anti_vowel(text):
    vlist = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    textl = list(text)
    
    for i in textl:
        if i in vlist:
            num = textl.index(i)
            textl.pop(num)
    newstr = "".join(textl)
    return newstr
    


.


#2

When destroying a list we are iterating, great care needs to be taken so that removing elements does not interfere with the iteration.

vlist = "aeiouAEIOU"
def anti_vowel(text):
    textl = list(text)
    for i in range(len(textl)-1, 0, -1):
        if textl[i] in vlist:
            textl.pop(i)
    return "".join(textl)

print anti_vowel("Hey look, words!")   # Hy lk, wrds!

Because we start on the right side, the iterator is going downward to zero, so the popped index does not affect the iteration of remaining untested indices.