[Python-ideas] Accessing the result of comprehension's expression from the conditional

Lie Ryan lie.1296 at gmail.com
Sat Jun 20 22:29:28 CEST 2009

Georg Brandl wrote:
> Lie Ryan schrieb:
>> Terry Reedy wrote:
>>> Steven D'Aprano wrote:
>>>> On Sat, 20 Jun 2009 08:31:26 am Lie Ryan wrote:
>>>> You don't like lambda? Fine, define an external function first. Then
>>>> you can write:
>>>> filter(pred, (f(x) for x in seq))
>>>> There's no violation of DRY, there's no redundancy, there's no lambda,
>>>> there's no "y" variable needed. What's ugly about it?
>>> I think its great and that it kills any justification for the proposal.
>>> tjr
>> I hate it. It mixes map/filter style and comprehension style; and the
>> fact it does so in a single line only makes it worse. Not that it would
>> be any better in two lines.
> Taking this further, using only map/filter style like this
> filter(pred, map(f, seq))
> takes two steps. Why is it so bad that doing it in a listcomp
> (el for el in (f(y) for y in seq) if el > 2)
> takes two steps as well?
> Georg

Comprehension, by its nature, is map+filter in a single expression.
Nested comprehension is (map+filter)+(map+filter).

(where + is some sort of function composition)

The proposal enables comprehension to become filter+map+filter,
map+filter, filter+map, or map-only; eliminating the redundant map in

The filter() and map() functions are two separate function in the first
place, so there is no redundancy in it.

Mixing the two styles is ugly since it means you have to think in two
separate (though related) paradigms.

More information about the Python-ideas mailing list