On Fri, Apr 10, 2020 at 11:20:55PM +0100, haael wrote:
Let the construction:
for element in iterable if condition: loop_body
mean:
for element in iterable: if not condition: continue loop_body
Fortuntely, it is already possible to write that in Python today without the negative logic and `continue`. for element in iterable: if condition: loop_body Benefits of the existing syntax: - It uses composition of simple building blocks instead of a specialised syntax that has to be learned. Once you have learned "for loops" and "if statements" you can combine them and pretty much everyone will know what they mean, without having to learn dedicated syntax. - Not ambiguous: there is no question whether the "if" part applies to the entire loop or each iteration. # Apply the if statement to the entire loop. if condition: for element in iterable: loop_body # Apply the if statement to each iteration of the loop. for element in iterable: if condition: loop_body - Backwards compatible all the way back to Python 1.5. - Makes the condition more visible (brings it to the front of the line, instead of hiding it way at the end of the `for`. - If you are paid by the line, you earn more. Cons: - Uses one extra line. - Uses one extra indent. - Uses one extra colon. If the `;` key on your keyboard is broken you will have to copy and paste it from elsewhere. -- Steven