1

I am loading a file "data.imputation" which is 2 dimensional in variable 'x'. Variable 'y' is a copy of 'x'. I pop the first array from 'y' (y is 2D). Why is the change reflected on x? (The first array from 'x' is also popped)

ip = open('data.meanimputation','r')
x = pickle.load(ip)
y = x
y.pop(0)

At the start, len(x) == len(y). Even after y.pop(0), len(x) == len(y). Why is that? And how can I avoid it?

2 Answers 2

4

use y = x[:] instead of y = x. y = x means both y and x are now pointing to the same object.

Take a look at this example:

>>> x=[1,2,3,4]
>>> y=x
>>> y is x
True          # it means both y and x are just references to a same object [1,2,3,4], so changing either of y or x will affect [1,2,3,4]
>>> y=x[:]     # this makes a copy of x and assigns that copy to y,
>>> y is x     # y & x now point to different object, so changing one will not affect the other.
False

If x is a list is list of list then [:] is of no use:

>>> x= [[1,2],[4,5]]
>>> y=x[:]   #it makes a shallow copy,i.e if the objects inside it are mutable then it just copies their reference to the y
>>> y is x
False         # now though y and x are not same object but the object contained in them are same 
>>> y[0].append(99)
>>> x
[[1, 2, 99], [4, 5]]
>>> y
[[1, 2, 99], [4, 5]]
>>> y[0] is x[0]
True  #see both point to the same object

in such case you should use copy module's deepcopy() function , it makes non-shallow copies of object.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. It works. Could you explain what is happening? And what is the difference?
y = list(x) is easier to understand syntax that means same as y=x[:]
@lostboy_19 it looks like your list is a list of list, so added some explanation for that too.
1

y = x does not copy anything. It binds the name y to the same object already referred to by x. Assignment to a bare name never copies anything in Python.

If you want to copy an object, you need to copy it explicitly, using whatever methods are available for the object you're trying to copy. You don't say what kind of object x is, so there's no way to say how you might be able to copy it, but the copy module provides some functions that work for many types. See also this answer.

3 Comments

It depends on what kind of object x is. It will work if x provides such a method (Python dictionaries do, for instance). You could also try import copy and then y = copy.copy(x). But that again will only work if x is set up to be copied in that way. There is no generic way to "copy anything" in Python; you need to know what kind of thing you are copying, and know how to copy it.
x is a list. eg x = [[1,2],[4,5]]
If it's a list of lists like that, you'll need to use copy.deepcopy(x). Read the documentation for the copy module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.