12. purify error


this is my code.
Oops, try again. Your function crashed on [4, 5, 5, 4] as input because your function throws a "list index out of range" error.

how to handle this error ,what am i doing wrong?

def purify(l):
    for i in range(len(l)):
        if l[i]%2!=0:
            del l[i]       
    return l


del is a difficult approach to solve this problem

well, for [4, 5, 5, 4] the length is 4, but lists are zero indexed based so the highest index is 3, so for i=4 you will get an index error


in range(len(l)) , it will return i as 0,1,2,3 ant not 4.


oh yes, you are right

but 3 is also a problem, after removing a 5 from your list 2 is the highest index, so index 3 is still a problem


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