[Python-ideas] A conditional "for" statement

Michael S. Gilbert michael.s.gilbert at gmail.com
Fri Apr 24 00:49:29 CEST 2009


On Thu, 23 Apr 2009 15:34:58 -0700, Chris Rebert wrote:
> I don't see how this is clearer than either of the obvious alternatives:

The purpose would also be efficiency.  It's less efficient to check the
condition m==2 during every loop iteration than it is to set up a list
that excludes m=2 to begin with.

> It's certainly /slightly/ shorter, but the problem is not severe
> enough to warrant new syntax, imho.
> Also, this uses the `with` keyword in a completely different way from
> its existing use, which could be confusing.

Right, like I said, that's debatable, and "if" probably makes more
sense since it mimicks the list comprehension syntax anyway.  Example:

  for m in range( 0 , 5 ) if m != 2:
    print m
vs

  [m for m in range( 0 , 5 ) if m != 2 ]:

Mike



More information about the Python-ideas mailing list