Lists & two dimensions

Pichai Asokan asokanp at virtusa.com
Sat Jul 20 09:46:37 EDT 2002


I am a trainer and in a training session a participant came up with a
problem that stumped me:

Here is the code snippet that isolates what I want:
--------------------
board = [[-1] * 3 ]*3
print board
board[1][2] = 'x'
print board
--------------------
Output
-----------------------
[ [-1, -1, -1 ], [-1, -1, -1 ], [-1, -1, -1 ] ]
[ [-1, -1, 'x'], [-1, -1, 'x'], [-1, -1, 'x'] ]
-----------------------

I thought 
board = [[-1] * 3 ]*3
should give me a list of 9 elements;
but ...

What is going on?
Where can we read more to understand what is goingg on?

P Asokan



More information about the Python-list mailing list