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

Raymond Hettinger python at rcn.com
Thu Jun 18 22:43:09 CEST 2009


[Lie Ryan]
> In list/generator comprehension, currently we have no way to access the
> result of the expression and have to write something like this:
> 
> [f(x) for x in l if f(x) > 0]
> 
> if f() is heavy or non-pure (i.e. have side effects), calling f() twice
> might be undesirable.

[x for x in map(f, l) if x > 0]


Raymond




More information about the Python-ideas mailing list