[Tutor] simple question about lists (original) (raw)

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Jul 21 00:01:56 CEST 2004


char board[3][3]

I can't do multi dimensional lists in python can I? Am I right in thinking that I would need to either use a one dimensional list or get something like numeric to do this? Thanks in advance.

You can do multi dimensional lists in Python, but there are a few gottchas to watch out for because Python holds everything as references (pointers to an old C hand) so you can wind up referencing the same object from multiple places if you aren't careful.

The documentation warns of this.

But otherwise

row1 = [1,2,3] row2 = [4,5,6]

matrix = [row1,row2]

print matrix[0][1] # prints 2

You can also initialise in one step:

m2 = [ [1,2,3], [4,5,6] ]

HTH,

Alan G.



More information about the Tutor mailing list