I think of 2 options to do this task. however neither goes on well. For the first option, the error is "IndexError: list index out of range". For the second option, if I test a list a=[1,2,3,4,5,3,1], the result is [2, 4, 3]. Both 3 and 1 show 2 times in the original list, how come the purified list still has one 3 but no 1!!!! My best guess is that .remove(i) only remove the first value (i) in the list. But since this .remove(i) is in the loop, after the first "3" is removed, the second "3" will become the first "3" in the next round. Thus how come there is still 3 in the output. Any one can help point out the errors?
def purify(x): for i in range(0,len(x)): if x[i]%2==1: x.remove(x[i]) def purify(x): for i in x: if i%2==1: x.remove(i)