Purify - Why this doesn't works?



I'm stuck at the purify exercice of the python course. My code doesn't works, and I don't understand why.

it returns "Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4]. "

It seems it looks like the index is shifting. But I don't understand why, since I'm writing on one other list than the one used in the for. I'm really lost.


def purify(x):
    y = x
    for item in x:
        if(item % 2 != 0):
    return y

Thanks in advance.



y = x

will not make a copy of a list, it just creates a variable pointing at the same list



How could I make a copy of this list ?


you could use a for loop and append, or use the built in list() function


Does this mean, that if I change x that y also will be changed?
Or what's the matter, weather I use .copy or y=x


well, yes and no. Both variable point to the same list. So any changes made to the list, will be shown by both variables


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