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. Advertisements

  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. Advertisements

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. Roman Zeilinger

    For Loop Generate Statement

    Roman Zeilinger, Sep 23, 2004, in forum: VHDL
    Replies:
    2
    Views:
    3,714
    Allan Herriman
    Sep 23, 2004
  2. Replies:
    3
    Views:
    1,060
  3. Jay McGavren
    Replies:
    11
    Views:
    1,460
    Alan Krueger
    Jan 16, 2006
  4. tedsuzman
    Replies:
    2
    Views:
    7,573
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  5. Ted
    Replies:
    1
    Views:
    676
    Duncan Booth
    Jul 22, 2004
  6. Replies:
    21
    Views:
    1,388
    Giannis Papadopoulos
    Aug 2, 2005
  7. bthumber
    Replies:
    5
    Views:
    608
    Alexey Smirnov
    Jan 29, 2009
  8. Isaac Won
    Replies:
    9
    Views:
    798
    Ulrich Eckhardt
    Mar 4, 2013
Loading...