List Comprehension Syntax

Peter Hansen peter at engcorp.com
Sun Jul 11 08:35:35 EDT 2004


Moosebumps wrote:

> Just curious -- anyone care to tell me how they would format the above?  (or
> maybe you wouldn't write it all)

result = [x for x in range(10)
     if x % 2 == 0
     if x % 3 == 0
     ]

result = [x * y
     for x in range(10) if x % 2 == 0
     for y in range(10) if y % 3 == 0
     ]

Probably something like that?

-Peter



More information about the Python-list mailing list