List comprehensions

Mike Fletcher mfletch at tpresence.com
Tue Dec 21 11:05:57 EST 1999


How about:

comprehension := "[" headclause forclause+ ]
headclause := expression ","?
forclause := "for" expression "in" expression ","?

So:

[ (x*2,x/2,x**y), 
	for x in somelist, 
	for y in someotherlist,
]
or
[ (x*2,x/2,x**y) 
	for x in somelist 
	for y in someotherlist
]
or even
[ x*2,x/2,x.__pow__(y) 
	for x in somelist 
	for y in someotherlist
]

Which seems to take care of most of the problems, it's got a decent sound
(especially with the commas present), is unambiguous as far as I can see,
and provides for parallel iteration.  While I prefer the explicit syntax of
the first example, (commas everywhere), allowing the syntax to be flexible
seems the best approach.

Just a thought...
Mike




More information about the Python-list mailing list