if statement in for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Lowe

    if statement in for loop

    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:
    [color=blue][color=green][color=darkred]
    >>> for x in y if x < 10 :[/color][/color][/color]

    is semantically equivalent to
    [color=blue][color=green][color=darkred]
    >>> for x in [x for x in y if x < 10] :[/color][/color][/color]

    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


  • Peter Hansen

    #2
    Re: if statement in for loop

    Ryan Lowe wrote:[color=blue]
    >
    > 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:
    >[color=green][color=darkred]
    > >>> for x in y if x < 10 :[/color][/color]
    >
    > is semantically equivalent to
    >[color=green][color=darkred]
    > >>> for x in [x for x in y if x < 10] :[/color][/color]
    >
    > 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?[/color]

    Seems unnecessary:

    for x in y:
    if x < 10:
    continue

    blah blah...


    -Peter

    Comment

    Working...