12 - purify can someone please help me


#1

My code doesnt work it only partially works but cant understand exactly why can someone please help me.
Feel like maybe i made the code too simple.
thank you in advance for any help.


#2

Hi,

you are removing items from the list you are iterating over.

When calling purify([4,5,5,4] the first item checked is 4, but since 4%2 is 0 it won't get removed. The second number is 5 and it will get removed. Now the third number is 4 again, because you changed lst to [4, 5, 4] by removing one 5. 4 won't be removed and the loop exits.


#3

how can i correct that ?


#4

Like @eveat said, iterating over a list and altering values from it won't make you get the correct output.
Try making a copy of that list by doing:

newList = oldList[:]

Q: But I'm doing that already, am I?
A: No, in this case you're just passing the reference of it which means that if you alter it anything on your list inside your fuction, the original one will be altered as well.


#5

oh i see thanks. I got it to work now. Thanks again for your help