Multi-dimensional list initialization

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Nov 7 06:11:15 EST 2012


On Nov 7, 2012 5:41 AM, "Gregory Ewing" <greg.ewing at canterbury.ac.nz> 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]]

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121107/219f8d28/attachment.html>


More information about the Python-list mailing list