[Python-ideas] if-statement in for-loop

Dominik Gresch greschd at gmx.ch
Sun Sep 11 05:36:08 EDT 2016


Hi,

I've recently found myself writing code similar to this:

for i in range(10):
     if i == 5:
         continue
     "body"

which I find a bit ugly. Obviously the same could be written as

for i in range(10):
     if i != 5:
         "body"

but here you would have to look at the end of the body to see if 
something happens when i==5.
So I asked myself if a syntax as follows would be possible:

for i in range(10) if i != 5:
     body

Personally, I find this extremely intuitive since this kind of 
if-statement is already present in list comprehensions.

What is your opinion on this? Sorry if this has been discussed before -- 
I didn't find anything in the archives.

Best regards,
Dominik Gresch


More information about the Python-ideas mailing list