Conditional iteration
Paul Rubin
http
Thu Dec 14 02:26:44 EST 2006
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