<div dir="ltr"><div>Hi all,</div><div><br></div><div>Often I have typed something like</div><div><br></div><div>    for x in range(100) if is_prime(x):</div><div>        # do things with x</div><div><br></div><div>to find that this does not work, instead resorting to:</div><div><br></div><div>    for x in range(100):</div><div>        if is_prime(x):</div><div>            # do things with x</div><div><br></div><div>or</div><div><br></div><div>    for x in range(100):<br></div><div>        if not is_prime(x):<br></div><div>            continue<br></div><div>        # do things with x<br></div><div><br></div><div>Other solutions to another case of this 'problem' are discussed has been discussed on StackOverflow (<a href="http://stackoverflow.com/questions/6981717/pythonic-way-to-combine-for-loop-and-if-statement">http://stackoverflow.com/questions/6981717/pythonic-way-to-combine-for-loop-and-if-statement</a>) where it is suggested one uses a generator expression before the loop. None of these solutions seem very Pythonic to me.</div><div><br></div><div>I appreciate there is a cost associated with changing the language syntax, and I do not understand all the finer details of the inner workings involved with the Python language development, however in my limited understanding in it would be:</div><div>- fully backwards compatible,</div><div>- require one to change "expression_list" to "or_test [comp_iter]" in the syntax of the for statement (if I got it right).</div><div>- it would mean there is a Pythonic solution to a current 'problem' that does not have one.</div><div><br></div><div>A few problems I foresee:</div><div>- One wants for loops to bleed their target_list (that is the point normally), so this is different from generators,</div><div>- This allows for nesting of generators, like in a generator expression which might be hard to implement?</div><div><br></div>Note that this has been suggested before at least once (<a href="https://mail.python.org/pipermail/python-dev/2007-November/075257.html">https://mail.python.org/pipermail/python-dev/2007-November/075257.html</a>), and that thread itself suggests it has been suggested before and shutdown by Guido (though no source is given for this).<div><br></div><div>All the best,</div><div><br></div><div>Henk-Jaap Wagenaar</div></div>