Python 2D list has weird behavor when trying to modify a single value...

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-04-29T17:48:14Z Indexed on 2010/04/29 17:57 UTC
Read the original article Hit count: 320

Filed under:
|
|
|
|

Hi guys,

So I am relatively new to Python and I am having trouble working with 2D Lists.

Here's my code:

data = [[None]*5]*5
data[0][0] = 'Cell A1'
print data

and here is the output (formatted for readability):

[['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None]]

Why does every row get assigned the value?

© Stack Overflow or respective owner

Related posts about python

Related posts about list