2D lists

Manuel M. Garcia mgarcia at cole-switches.com
Mon Jan 20 16:48:21 EST 2003


On Mon, 20 Jan 2003 12:38:32 +0800, Marcin Matuszkiewicz
<marcin at finisar.com> wrote:
>What is a most efficient way to create an intialized 2D list?

[ [0] * y for _ in range(x) ] is not inefficient.  What is your
application?

For small arrays I would just type it out:
[ [0,0], [0,0], [0,0] ]

Python is a scripting language, so naive use of Python will not get
the same high level of performance you would get if you coded in C or
C++.

That said, making use of the Numerical Python extensions, you can do
many array and matrix operations at C speed.  Numerical Python is used
by people who wish to do "heavy-duty" numerical analysis in Python.

http://www.pfdubois.com/numpy/

Between tools to help you write your own C extension, and new
techniques for compiling Python, you should be able to tame any speed
critical algorithm.

Manuel




More information about the Python-list mailing list