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

Aahz aahz at pythoncraft.com
Thu Jun 18 22:22:21 CEST 2009

On Fri, Jun 19, 2009, Lie Ryan wrote:
> 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.

Listcomps and genexps are like lambdas: run up against their limits and
you should switch to a regular for loop or generator.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha

More information about the Python-ideas mailing list