how to define the size of a list?

Gerhard Häring gerhard.haering at gmx.de
Thu Nov 7 17:50:35 EST 2002


Marco Herrn wrote in comp.lang.python:
> Sorry if that is a stupid question, but I didn't find the answer in the
> docu.

It is in the FAQ at pyhon.org :-)

> I want to define a matrix (a 2-dimensional list) with a specific size.
> But I didn't find a way to set the size of the list at creation time.

There is no direct way to do this: Python doesn't have
multi-dimensional arrays. What you probably want is a list of lists.

Note that the tempting

    x = [[None] * 4] * 4
    x[1][1] = 5
    print x

will not produce the desired result. The FAQ entry eplains why this is
so.

HTH,

-- Gerhard



More information about the Python-list mailing list