extended list comprehensions

Bernhard Herzog bh at intevation.de
Mon May 27 09:15:54 EDT 2002


fxj at hotmail.com (F. Jamitzky) writes:

> It would be great to have something like a list comprehension for the
> reduce function. It would work in the same way as the comprehension
> for the map function, which is:
> 
> [foo(x) for x in xs]      <-is the same as->       map(foo,xs)
> 
> and maybe it could be written as:
> 
> {y=y+x for x in xs}      <-would be->         reduce(operator.add,xs)


Well, one could mimic reduce with the side-effects of a list
comprehension:

>>> y = 0
>>> xs = range(10)
>>> [0 for x in xs for y in [y + x] if 0]
[]
>>> y
45

Not recommended, though :-)


   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list