[Python-Dev] List comprehension syntax (Features for Python 2.0)

M.-A. Lemburg mal@lemburg.com
Tue, 25 Jul 2000 18:59:55 +0200

Guido van Rossum wrote:
> > I would also like to volunteer to repeat the "simultaneous loop" syntax
> > experiment with list comprehensions, i.e. see whether what's being
> > proposed lines up with people's pre-existing intuitions.
> Please do!
> Though I have no doubt of the outcome:
>   [x+y for x in [10, 20, 30] for y in [1, 2, 3]]
> will be almost universally understood to mean
>   [11, 12, 13, 21, 22, 23, 31, 32, 33].

Just a note about the syntax: why does the above have
the expression and the "for" notation reversed ?

Wouldn't it make much more sense to write something
more lambda function like, e.g.

    [for (x,y) in zip([10,20,30], [1,2,3]): x+y]


    [for x in range(100): x*2]


I believe the above would cause much less confusion since
it resembles standard Python "for" loop notation (making it
intuitive for Python programmers) and is in syntax very
similar to what "lambda" uses:

    lambda <vars>: <evaluated expr>

List comprehension would then become something like:

    [for <vars> in <expr>: <evaluated expr>]

Marc-Andre Lemburg
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/