if statement in for loop

Discussion in 'Python' started by Ryan Lowe, Aug 20, 2003.

  1. Ryan Lowe

    Ryan Lowe Guest

    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
    Ryan Lowe, Aug 20, 2003
    #1
    1. Advertising

  2. Ryan Lowe

    Peter Hansen Guest

    Ryan Lowe wrote:
    >
    > 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?


    Seems unnecessary:

    for x in y:
    if x < 10:
    continue

    blah blah...


    -Peter
    Peter Hansen, Aug 20, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    783
  2. Jay McGavren
    Replies:
    11
    Views:
    1,097
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,051
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    442
    Duncan Booth
    Jul 22, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    342
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page