Nested List Comprehension

Burkhard Kroesen kroesen at T-Online.de
Mon Jul 16 09:50:15 CEST 2001


James T. Dennis wrote:

>  This is not a question but merely a comment from a 
>  newbie.   I'm reading Beazley's 2nd Ed. "Python Essential
>  Reference" and playing with things therein and I thought
>  "list comprehension, what a quick way to make a multiplication 
>  table"
> 
>  multtab = [ [ x*y for x in range(13) ] for y in range(13) ]
> 
>  ... though I was surpirsed that I got the syntax correct
>  on the first try.  (It still looks wrong, somehow but 
>  [ x*y [x for x in range(13)] [y for y in range(13) ] ] looks
>  even more wrong).

Maybe you feel more comfortable with
multtab = [x*y for x in range(13) for y in range(13)]

Burkhard Kroesen




More information about the Python-list mailing list