2D lists

Marcin Matuszkiewicz marcin at finisar.com
Mon Jan 20 14:06:47 EST 2003


In the code below I created a two dimensional list in two ways.  List
b behaves I expect, list a does not.  Could someone explain it?

>>> a=[[0]*4]*2
>>> a
[[0, 0, 0, 0], [0, 0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0, 0], [1, 0, 0, 0]]
>>> b=[[0, 0, 0, 0], [0, 0, 0, 0]]
>>> b[0][0]=1
>>> b
[[1, 0, 0, 0], [0, 0, 0, 0]]
>>>

Thanks,

Marcin




More information about the Python-list mailing list