how to make a nested list

John Ladasky ladasky at my-deja.com
Thu Sep 15 14:43:33 EDT 2011


Stef,

Are your bottom-level lists always of length 2?  If so, then you could
use an array, instead of a list of lists.

Python ships with a module called array, but it doesn't allow you to
put non-numeric types into arrays, and it looks like you want the
NoneType.  I use the popular numpy module, which does allow non-
numeric types.

You might also like the slice notation that numpy uses for referencing
items in the array.  The indices go inside a single set of square
brackets, and are separated by commas.

>>> from numpy import empty
>>> B = empty((3,2), object)
>>> B
array([[None, None],
       [None, None],
       [None, None]], dtype=object)
>>> B[2,0] = 77
>>> B
array([[None, None],
       [None, None],
       [77, None]], dtype=object)



More information about the Python-list mailing list