# Purify!

#1

where am i going wrong?it prints:[4,5,4] please explain me!

def purify(num):

``````for x in num:
if x%2==1:
num.remove(x)

return num``````

print purify([4,5,5,4])

#2

well, `.remove()` is a difficult approach given you deal with index shifts which causes succesive odd numbers to be skipped

go for the opposite approach, append even numbers to a new list

#3

i did it this way by cloning my list and it's working ,but i dont know why?
def purify(num):
t=num[:]

``````for x in t:
if x%2==1:
num.remove(x)

return num``````

print purify([4,5,5,4])

#4

because you remove from a different lists then you are removing from, you don't suffer from the index shifts

#5

could you please elaborate "INDEX SHIFT"?

#6

well, lists can't have empty indexes, so the moment you remove the first `5` from the list, everything to the right of this `5` shifts to the left while the loop moves on to the next index, this cause the second `5` to be skipped

#7

ok i have got it now ,thanxs

#8

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