Anti-Vowel: Why is it wrong?


What's wrong with my code?

def anti_vowel(text):
    to_list = list(text)
    vowel = ["a","e","i","o","u","A","E","I","O","U"]
    for i in range(len(to_list), -1, -1):
        if to_list[i] in vowel:
            to_list = to_list - to_list[i]
    return ("").join(to_list)

print anti_vowel("Hey look Words!")

Traceback (most recent call last):
File "python", line 9, in
File "python", line 5, in anti_vowel
IndexError: list index out of range



for i in range(len(to_list), -1, -1):

strings are zero indexed based (first index is zero) while len() start counting at one. so you will need to subtract one from length, otherwise you get an index error

to remove based on index, use del


@aquaphoenix17 with two little fixes, the problem is solved. Why recommend a completely different approach?


Hence why I deleted the post.


I am sorry but i don't understand.

Is it like this?:

For i in range(len(to_list) - 1, -1, -1):

However, i got an error for this.

How to use the del to subtract one from length?


keywords like for are case sensitive, for should be all lowercase

please post your full code


What I mean is how you use the "del" to solve my error(subtract one from the list length)?


for how to use built-in function documentation was invented


I don't understand, please clarifty


Please see the Python documentation for how to use del. The documentation for Python 2.7 can be found here.


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