Multi-dimensional list initialization

Oscar Benjamin oscar.j.benjamin at
Wed Nov 7 12:11:15 CET 2012

On Nov 7, 2012 5:41 AM, "Gregory Ewing" <greg.ewing at> wrote:
> If anything is to be done in this area, it would be better
> as an extension of list comprehensions, e.g.
>   [[None times 5] times 10]
> which would be equivalent to
>   [[None for _i in xrange(5)] for _j in xrange(10)]

I think you're right that the meaning of list-int multiplication
can't/shouldn't be changed if this way.

A multidimensional list comprehension would be useful even for people who
are using numpy as it's common to use a list comprehension to initialise a
numpy array.

A more modest addition for the limited case described in this thread could
be to use exponentiation:

>>> [0] ** (2, 3)
[[0, 0, 0], [0, 0, 0]]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list