Multidimensional arrays/lists

Rhodri James rhodri at wildebst.demon.co.uk
Sun Sep 27 15:46:14 EDT 2009


On Sun, 27 Sep 2009 17:40:57 +0100, Someone Something  
<fordhaivat at gmail.com> wrote:

> I'm trying to write a little tic-tac-toe program I need a array/list such
> that I can represent the tic tac toe board with an x axis and y axis and  
> i
> can access each square to find out whether there is an X or an O. I have
> absolutely no idea how to do this in python and I really, really, don't  
> want
> to do this is C.

A list of lists.

>>> board = [[None, None, None], [None, None, None], [None, None, None]]
>>> board[0][0] = 'X'
>>> print board[0][1]
None

Just be a little careful constructing your board.  It's awfully easy to
end up with rows being the same list object if you're careless, and then
unexpected things happen :-)

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list