Ideas about optional filter on for loop statements

Discussion in 'Python' started by Brian L., May 11, 2004.

  1. Brian L.

    Brian L. Guest

    After following the discussion on Generator expressions on python-dev
    for the last few weeks, and finally getting used to list
    comprehensions as opposed to map/filter, it occurred to me that the
    for loop statement lacks the filtering feature when compared to the
    listcomp/genexp. For instance,

    (real code)
    for x in range(10):
    if x%2 is 0:
    print x

    Could be reduced to: (fake code)

    for x in range(10) if x%2 is 0:
    print x

    Which is comparable to iterating and printing the elements of the
    listcomp:
    [x for x in range(10) if x%2 is 0]

    or the genexp: (this syntax isn't real yet, but I'd imagine this to be
    fairly accurate based on python-dev)

    (x for x in range(10) if x%2 is 0)

    Note the perfect consistency between all three iterator usages.

    This change would not break any compatibility that I can think of, but
    would improve self-consistency within python. Furthermore, if
    listcomps and genexps are a direction that python is moving in, then
    it would probably make sense that the for loop statement has an
    optional filtering clause as well.

    The strongest argument I can think of against it is that for loop
    statements should be reserved for iteration only, and that they
    shouldn't have functional features (for instance, the for loop can not
    and will not ever have 'map' like semantics, so why should it have
    filter?). I don't think, however that this is a problem, and
    considering the number of times that I've written loops in the
    following patterns:

    for x in l:
    if x is None: continue
    ...

    or

    for x in l:
    if x is not None:
    ...

    I think that a more pythonic syntax may be in order.

    for x in l if x is not None:
    ...

    is certainly cleaner and easier to read. This clause should of course
    be optional

    Thoughts?

    If this receives enough positive attention on comp.lang.python, then
    I'll write it up and submit a PEP.

    Brian
     
    Brian L., May 11, 2004
    #1
    1. Advertising

  2. Brian L.

    Larry Bates Guest

    Brian,

    How about writing:

    for x in [i for i in range(10) if i%2 is 0]:
    print x

    or in your other examples:

    for x in [i for i in l if i]:
    ....

    for x in [i for i in l if not i]:
    ....

    All seem exteremely close to your proposal but
    don't require any changes to current language
    implementation (e.g. just use list comprehensions).

    Regards,
    Larry Bates
    Syscon, Inc.

    "Brian L." <> wrote in message
    news:...
    > After following the discussion on Generator expressions on python-dev
    > for the last few weeks, and finally getting used to list
    > comprehensions as opposed to map/filter, it occurred to me that the
    > for loop statement lacks the filtering feature when compared to the
    > listcomp/genexp. For instance,
    >
    > (real code)
    > for x in range(10):
    > if x%2 is 0:
    > print x
    >
    > Could be reduced to: (fake code)
    >
    > for x in range(10) if x%2 is 0:
    > print x
    >
    > Which is comparable to iterating and printing the elements of the
    > listcomp:
    > [x for x in range(10) if x%2 is 0]
    >
    > or the genexp: (this syntax isn't real yet, but I'd imagine this to be
    > fairly accurate based on python-dev)
    >
    > (x for x in range(10) if x%2 is 0)
    >
    > Note the perfect consistency between all three iterator usages.
    >
    > This change would not break any compatibility that I can think of, but
    > would improve self-consistency within python. Furthermore, if
    > listcomps and genexps are a direction that python is moving in, then
    > it would probably make sense that the for loop statement has an
    > optional filtering clause as well.
    >
    > The strongest argument I can think of against it is that for loop
    > statements should be reserved for iteration only, and that they
    > shouldn't have functional features (for instance, the for loop can not
    > and will not ever have 'map' like semantics, so why should it have
    > filter?). I don't think, however that this is a problem, and
    > considering the number of times that I've written loops in the
    > following patterns:
    >
    > for x in l:
    > if x is None: continue
    > ...
    >
    > or
    >
    > for x in l:
    > if x is not None:
    > ...
    >
    > I think that a more pythonic syntax may be in order.
    >
    > for x in l if x is not None:
    > ...
    >
    > is certainly cleaner and easier to read. This clause should of course
    > be optional
    >
    > Thoughts?
    >
    > If this receives enough positive attention on comp.lang.python, then
    > I'll write it up and submit a PEP.
    >
    > Brian
     
    Larry Bates, May 11, 2004
    #2
    1. Advertising

  3. Brian L.

    Duncan Booth Guest

    (Brian L.) wrote in
    news::

    > for x in l if x is not None:
    > ...
    >
    > is certainly cleaner and easier to read. This clause should of course
    > be optional
    >
    > Thoughts?


    IMHO:

    for x in l:
    if x is not None:

    is cleaner and easier to read than forcing everything onto one line. In
    most cases the names or expressions would be longer (and hopefully
    meaningful), so you would want to split the 'for' and 'if' onto two
    separate lines anyway.

    Besides, these days you can write a generator trivially, so just put the
    condition inside a generator, invent a meaningful name for it and then you
    don't have to return None as a value at all.
     
    Duncan Booth, May 12, 2004
    #3
  4. (Brian L.) wrote in message news:<>...

    > The strongest argument I can think of against it is that for loop
    > statements should be reserved for iteration only, and that they
    > shouldn't have functional features (for instance, the for loop can not
    > and will not ever have 'map' like semantics, so why should it have
    > filter?). I don't think, however that this is a problem, and
    > considering the number of times that I've written loops in the
    > following patterns:
    >
    > for x in l:
    > if x is None: continue
    > ...
    >
    > or
    >
    > for x in l:
    > if x is not None:
    > ...
    >
    > I think that a more pythonic syntax may be in order.


    Remember how many times you've written something like this:

    if condition:
    return

    Wouldn't it be great to have a single-line "return if condition"? ;)
    But what about "... preferably only one ... obvious way to do it"?

    >
    > for x in l if x is not None:
    > ...
    >
    > is certainly cleaner and easier to read.


    Not sure about that. Sometimes i break list comprehensions
    into several lines to make them more readable.

    - kv
     
    Konstantin Veretennicov, May 12, 2004
    #4
    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. Neil Zanella
    Replies:
    8
    Views:
    1,197
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    393
    Bart Nessux
    Feb 23, 2004
  3. Vince
    Replies:
    12
    Views:
    754
    Martin Gregorie
    Jan 21, 2008
  4. John Crichton
    Replies:
    6
    Views:
    267
    John Crichton
    Jul 12, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    391
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page