Nested List Comprehension

Steve Holden sholden at holdenweb.com
Wed Jul 18 05:20:20 CEST 2001


"Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
news:3B54FAA2.D7E04ECB at cosc.canterbury.ac.nz...
> Burkhard Kroesen wrote:
> >
> > Maybe you feel more comfortable with
> > multtab = [x*y for x in range(13) for y in range(13)]
>
> But that doesn't produce what he wanted, which
> was for multtab[x][y] to be equal to x*y.
>
> But if there were such a thing as dictionary
> comprehensions, he could use
>
>   multtab = {(x,y):x*y for x in range(13)
>                          for y in range(13)}
>
> :-)

>>> multtab = {}
>>> [multtab.update({(x, y): x*y}) for x in range(13) for y in range(13)]
[None, None, ...,  None]
>>> multtab[6,8]
48

If I could think of a way to recycle those Nones I might make money here ;-)

regards
 Steve
--
http://www.holdenweb.com/






More information about the Python-list mailing list