I know appending the even numbers in the list to a new list would work but I just want to find out why this method does not work. The code only removes a few odd numbers instead of all. What is the reason behind this occurrence? Thanks in advance for any help!
Replace this line with your code. def purify(dirtylist): dl = dirtylist[:] ##create a new list so not to modify the original list for number in dl: if number % 2 != 0: dl.remove(number) print dl ##see the process return dl #just my own testing testlist = [4,5,5,5,5,5,5,5,5] print purify(testlist) print testlist ##test whether the original list is still there