Help! anti_vowel


#1


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):
    
    string = []
    
    for i in text:
        string.append(i)
    
    for i in string:
        if i == "a" or i == "A" or i == "e" or i == "E" \
        or i == "i" or i == "I" or i == "o" or i == "O" \
        or i == "u" or i == "U":
            string.remove(i)
    
    return "".join(string)


#2

.remove() is a difficult approach given you deal with index shifts (everything to the right of a removed item will shift one index to the left given list and strings can't have empty indexes)

this cause succesive vowels to be skipped, because the loop moves on to the next index

index shifts only occurs when you remove from the same list as you are looping over, so you could loop over text and then remove from string, that is fine.

Or you could go for the opposite solution, appending constanants to a new list


#3

instead of:
for i in string
because the remove function changes the index (as mentioned by stetim94), you can loop backwards so that the index doesn't matter

so:
for i in reversed(string)
would work :slight_smile:


#4

True, but .remove() will remove the first match it can find, so it feels fragile but i think that is a personal opinion


#5

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