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

M.-A. Lemburg mal@lemburg.com
Tue, 25 Jul 2000 23:45:25 +0200


Fredrik Lundh wrote:
> 
> mal wrote:
> > 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]
> >
> > or
> >
> >     [for x in range(100): x*2]
> 
> fwiw, I read that as:
> 
>     [
>         for x in range(100):
>             x * 2
>     ]
> 
> which makes no sense at all.

Why doesn't that make sense ? Is this just a gut feeling
or explainable ?

IMHO, the meaning of the differen parts is intuitively
understandable from the fact that Python's standard "for"
notation works in the same way. 

OTOH, the notation "x*2 for x in range(100)" looks strange to me
and probably to others too, because it doesn't make clear what "x"
really refers to until you read on... this is Python not Forth or
Haskell !

BTW, is it the missing append operator (the one that pushes
x*2 onto the list) that's bothering you ?

Note that we could also extend the above by allowing multiple
such constructs separated by commas:

data = (1.3, 1.2, 1.1)
l = [for x in data: 3*x, 
     for x in data: x/10]

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