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

spir denis.spir at free.fr
Fri Jun 19 17:08:32 CEST 2009

Le Fri, 19 Jun 2009 09:45:08 -0400,
Jim Jewett <jimjjewett at gmail.com> s'exprima ainsi:

> On Fri, Jun 19, 2009 at 7:34 AM, Tal Einat<taleinat at gmail.com> wrote:
> > Just using [f(x) for x in nums if f(x) > 0] is the most readable and
> > obvious option.
> I would often prefer to break it into two steps:
>     temp = (f(x) for x in nums)
>     results = [e for e in temp if e>0]
> Others will dislike the extra line and temp var, which is one reason
> it isn't among the several solutions previously suggested.  Are these
> differences big enough (or the solutions obscure enough) that the
> variation is itself a cost of the current situation?

Ditto for me.

A list comp with both computation and filtering is for me two ideas, two steps, so I write two lines anyway. This also has the advantage to disambiguate the order issue (computation or filter first?) and there is no risk of double side-effect (which anyway I never use, but who knows...).

la vita e estrany

More information about the Python-ideas mailing list