Conditional iteration

at at at tuko.nl
Thu Dec 14 03:11:52 EST 2006


Hi Paul,

I appreciate your explanation!

Thanx

@

Paul Rubin wrote:

> at <at at tuko.nl> writes:
>> >   for x in (x for x in [-2, -1, 0, 1, 2, 3, 4] if x > 0):
>> >        ... more code ...
> 
>> Do you know if this generates a new list internally (memory consumption?)
> 
> It does not.  That parenthesized expression is a called generator
> expression.  It compiles to a small subroutine, more or less, that
> gets invoked repeatedly as you iterate through it.
> 
> A similar expression with square brackets is called a list
> comprehension and does generate a new list.




More information about the Python-list mailing list