[Tutor] Multidimensional arrays

Alan Gauld alan.gauld at blueyonder.co.uk
Tue May 18 19:22:27 EDT 2004


> I am sure I am missing something obvious here but I cannot 
> see how to set up a two-dimensional array. I am hoping it 
> can be done using lists of lists, as it were, 

Yes, a list of lists is the usual solution. 
What happened when you tried it?

The interactive prompt is a great place to try out ideas to 
see how they work:

>>> L1 = [1,2,3]
>>> L2 = [3,4,5]
>>> L2D = [L1,L2]
>>> print L2D[1][2]  # -> 5

Or directly:

>>> L2D = [[1,2,3],[3,4,5]]
>>> print L2D[0][1] # -> 2

Or neater layout:

>>> L2D = [ [1,2,3]
...         [3,4,5]
...       ]
>>>

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list