Purify; Error : Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4]


#1

def purify(num):
    for noz in num:
        if noz % 2 == 0:
            pass
        
        else:
            num.remove(noz)
            
    return num

Whats wrong?


#2

When you delete something from it, the items gets shifted from original list.

You can create a copy of original list and delete from that instead of changing original list.

to make a copy you can use slice or list() method.
like..

x = [1,2,3]

y = list(x)
 
# or

y = x[:]

#3

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