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

Jim Jewett jimjjewett at gmail.com
Fri Jun 19 15:45:08 CEST 2009


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?

-jJ



More information about the Python-ideas mailing list