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]]
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]]
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With