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?
use
y = x[:]instead ofy = x.y = xmeans bothyandxare now pointing to the same object.Take a look at this example:
If x is a list is list of list then
[:]is of no use:in such case you should use
copymodule’sdeepcopy()function , it makes non-shallow copies of object.