Multi-dimensional list initialization
Ethan Furman
ethan at stoneleaf.us
Wed Nov 7 09:47:53 EST 2012
Oscar Benjamin wrote:
> On Nov 7, 2012 5:41 AM, "Gregory Ewing" <greg.ewing at canterbury.ac.nz
> <mailto: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]]
What would happen with
--> [{}] ** (2, 3)
or
--> [my_custom_container()] ** (2, 3)
?
~Ethan~
More information about the Python-list
mailing list