# [Tutor] indexing lists

Sam Corder scorder@cinci.rr.com
Mon, 5 Jun 2000 09:53:02 -0400

```I think it would be easiest to make a class that wraps a single list.  It
would have methods that would allow you to access it by row column values
and to pull back full rows or columns and maybe even diagonal lines like
[(1,1), (2,2), (3,3)].  It was easiest to code this out for me instead of
using a list of lists.  I would think that internally it would be more
effiecient as well.

def GetCell(Row, Col):
return list[(Row * NumCols) + Col]

This will allow you to get at a coordinate in the "grid".  NumCols would be
2 in your case (0,1,2).  Wrapping this in a simple class will allow you to
get on with the rest of the program quickly.  If you need to change the
underlying implementation for some reason you will be safe as long as you
don't change the method interfaces.

-Sam

-----Original Message-----
Timothy Wilson
Sent: Monday, June 05, 2000 9:03 AM
To: tutor@python.org
Subject: [Tutor] indexing lists

Hi everyone,

I'm wondering how, if even possible, you'd find the index of a given number
in the following list of lists:

l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

I've been playing around with my little tic tac toe program, and I can't
decide whether to use this list of lists structure for the matrix or a
simpler [1, 2, 3, 4, 5, 6, 7, 8, 9] structure.

I think programming the strategy might be easier with the more 2-dimensional
list of lists. But finding which moves are possible involves determining
which places in the list are occupied by integers, not "X" or "O". I need to
do l.index(spam) for that. Unfortunately, I can't do l.index(5). I can do
l.index([1, 2, 3]), however. Is it possible to get at that 2nd layer of
lists through an index statement?

-Tim

--
Tim Wilson      | Visit Sibley online:         | Check out:
Henry Sibley HS | http://www.isd197.k12.mn.us/ | http://www.zope.org/
W. St. Paul, MN |                              | http://slashdot.org/
wilson@visi.com |   <dtml-var pithy_quote>     | http://linux.com/

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://www.python.org/mailman/listinfo/tutor

```