Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list of lists [duplicate]

Tags:

python

list

Possible Duplicate:
Unexpected feature in a Python list of lists

I want to make a list of lists in Python, but apparently this doesn't work, as changing one number changes several. Why is this, and how can I fix it?

>>> a = [[0]*3]*4
>>> a[0][0] = 1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
like image 985
Paul S. Avatar asked Oct 21 '12 02:10

Paul S.


2 Answers

What you've discovered is a classic Python pitfall.

x = [0]*3 is a list. No problem there, but [x]*4 creates a list with 4 references to the exact same list x. So modifying the first element, x, also modifies the other elements as well.

Instead, if you do this:

In [193]: a = [[0]*3 for i in range(4)]

then you get 4 distinct items in the list:

In [194]: a[0][0] = 1

In [195]: a
Out[195]: [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
like image 50
unutbu Avatar answered Sep 28 '22 17:09

unutbu


In Python, [x]*n creates a new list with n references to x. This is perfectly good to have if x is immutable (string, tuple, etc.), but it gets rather confusing if x is mutable since changes to one element affect x, and thus appear to affect all elements of the list.

So, the solution is to use a list comprehension to create n new things:

[x for _ in xrange(n)] # we use _ here as a "dummy variable"

In your case, it would look like

a = [[0]*3 for _ in xrange(4)]
like image 39
nneonneo Avatar answered Sep 28 '22 19:09

nneonneo