I'm given a practice to write a function in Python that takes a list as input which includes all integers . I should remove all odd numbers and let even numbers stay and return the list .
This is my function :
def purify (lst):
for key in lst:
if key % 2:
lst.remove(key)
return lst
When I run it these are the results I get : (here are some test cases)
print purify([1,2,3,4,5,6,7,8,9])
results :
1 3 5 7 9 [2,4,6,8]
Another test case : When I run using this line :
print purify ([4,5,5,4,5,5])
results :
4 5 4 5 [4,4,5,5]
I'm not sure what's happening , if I remove that "if" condition , then there is no problem (like this):
def purify (lst):
for key in lst:
print key
return lst
It prints all values in list and doesn't skip any
Am I doing something wrong?