Purify - Why this doesn't works?


#1



Hey,


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.

code:

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

Thanks in advance.


#2

this:

y = x

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


#3

Thanks

How could I make a copy of this list ?


#4

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


#5

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


#6

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


#7

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