98

X is a 2D array. I want to have a new variable Y that which has the same value as the array X. Moreover, any further manipulations with Y should not influence the value of the X.

It seems to me so natural to use y = x. But it does not work with arrays. If I do it this way and then changes y, the x will be changed too. I found out that the problem can be solved like that: y = x[:]

But it does not work with 2D array. For example:

x = [[1,2],[3,4]]
y = x[:]
y[0][0]= 1000
print x

returns [ [1000, 2], [3, 4] ]. It also does not help if I replace y=x[:] by y = x[:][:].

Does anybody know what is a proper and simple way to do it?

0

5 Answers 5

138

Using deepcopy() or copy() is a good solution. For a simple 2D-array case

y = [row[:] for row in x]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the list comprehension version. It works fine for a 2d array and is way faster that deepcopy(x).
88

Try this:

from copy import copy, deepcopy
y = deepcopy(x)

I'm not sure, maybe copy() is sufficient.

2 Comments

for 2D array of floats copy do not work, but deepcopy does work, thanks!
copy() is not sufficient, it is a shallow copy so the result will be a copy of references to the original row lists.
15

For 2D arrays it's possible use map function:

old_array = [[2, 3], [4, 5]]
# python2.*
new_array = map(list, old_array)
# python3.*
new_array = list(map(list, old_array))

2 Comments

I think you need to "cast" that, like new_array = list(map(list, old_array))
distance = list(map(lambda i: list(map(lambda j: j, i)), graph)). I've also seen this....
1

In your case(since you use list of lists) you have to use deepcopy, because 'The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.'

Note that sample below is simply intended to show you an example(don't beat me to much) how deepcopy could be implemented for 1d and 2d arrays:

arr = [[1,2],[3,4]]

deepcopy1d2d = lambda lVals: [x if not isinstance(x, list) else x[:] for x in lVals]

dst = deepcopy1d2d(arr)

dst[1][1]=150
print dst
print arr

2 Comments

"you have to use deepcopy" Not really. There's always a more complicated, but (maybe) faster way. See Ryan Ye's answer. Yours is fine, but this statement is not perfect.
@palsch as i can see there is no difference between our answers. Mine deepcopy1d2d doing the same and even more))))
0

I think np.tile also might be useful

>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
       [[0, 1, 2, 0, 1, 2]]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.