Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing 2D lists in Python: How to make deep copies of each row?

Tags:

python

Let's say I want to initialize a 2D Python list with all 0's, I'd do something like:

test = [[0.0] * 10] * 10

Then I start modifying values in the first list...

test[0][0] = 1.0

But that affects the first item of ALL lists for some reason:

print test

[[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]

What's going on with that? Why is it making a deep copy of the inner list, but a shallow copy of the outer list?

like image 529
Randy Olson Avatar asked Jun 13 '13 23:06

Randy Olson


People also ask

How do you replicate a list in Python?

Using the extend() method The lists can be copied into a new list by using the extend() function. This appends each element of the iterable object (e.g., another list) to the end of the new list. This takes around 0.053 seconds to complete.

Does list () create a deep copy?

You don't make a deep copy using list() . (Both list(...) and testList[:] are shallow copies.) You use copy. deepcopy(...) for deep copying a list.


1 Answers

Doing [[0.0] * 10] * 10actually creates multiple copies of the same list so modifying one will affect all of them:

>>> test = [[0.0] * 10] * 10
>>> [id(x) for x in test]     #see all IDs are same
[3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L, 3065020524L]

Try this:

>>> test = [[0.0]*10 for _ in xrange(10)]
>>> test[0][0] = 1.0
>>> test
[[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]

Integers/floats are immutable, while lists are mutable:

>>> x = y = []
>>> x is y       # Both point to the same object
True 
>>> x.append(1)  # list.extend modifies a list in-place
>>> x,y          # both references to [] can see the modification
([1], [1])
>>> x = y = 1
>>> x is y      #both points to the same object
True
>>> x+=1        # only x gets modified, it now points to a new object 2
>>> x,y         # y still points to the same object 1
(2, 1)
like image 84
Ashwini Chaudhary Avatar answered Sep 22 '22 12:09

Ashwini Chaudhary