if statement in for loop

Ryan Lowe ryanlowe0 at msn.com
Wed Aug 20 02:31:28 CEST 2003


i thought id ask here before wirting a PEP, if people thought it would be a
good enhancement to allow if clauses in regular for-statements like so:

>>> for x in y if x < 10 :

is semantically equivalent to

>>> for x in [x for x in y if x < 10] :

but is shorter and easier to read. basically, the if works as a filter for
the values in the iterator. its not a major change, purely syntactic sugar.
and clearly backwards-compatible.

seem reasonable?

ryan






More information about the Python-list mailing list