[Python-ideas] A conditional "for" statement
Michael S. Gilbert
michael.s.gilbert at gmail.com
Fri Apr 24 00:29:15 CEST 2009
Hello,
I've recently been working on some code where i am processing a
list, but excluding certain items. The solution is to use a list
comprehension in the "for" statement. An example is:
for m in [n for n in range( 0 , 5 ) if n != 2]
Determining what's going on here isn't immediately obvious (i.e.
what's this new variable n doing?). It would be nice to have a more
streamlined syntax such as:
for m in range( 0 , 5 ) with m != 2
which is much cleaner and obvious. It is also very much like the
list comprehension syntax (although i've changed "if" to "with", and
the better of the two is subject to personal opinion and debatable). I
would hope that the statements following "with" could be any conditional
expression.
This is just a thought I had while working today. Thank you for your
consideration.
Best Regards,
Mike
More information about the Python-ideas
mailing list