I have had precisely the same idea.
It feels better to make this feature general (if introduced at all) than make it specific to 'for' + 'if'. I think there would have to be a rule that any 'if' that appeared on a line with other suite-introducing-statements could not have a corresponding 'else'.
Best wishes
Rob Cliffe

On 02/03/2022 19:53, Jeremiah Paige wrote:
I have on a few occasions wanted a for..in..if statement and if it existed would have used it. However, I agree that the level of change a new statement type
brings to the language is probably too high for this feature.

But what if python lifted the newline requirement for blocks that contain
compound statements? That is, statements that end in a ':' can be followed by other statements that end in a ':' on the same line. AFAICT there would be no
ambiguity (to the parser; to humans, depends). Doing so would add the OPs
requested feature, though it would be two statements on one line with one extra character. It would also essentially bring the full comprehension syntax to for
loops since fors and ifs could be chained arbitrarily.

# for..if
for x in y: if x in c:
    some_op(x

# nested generator-like for
for line in doc: for word in line.split():
    spellcheck(word)

# side effect one-liner
for item in an_iterable: if condition(item): side_effect(item))


Regards,
Jeremiah

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LWPJKAN37ZV7G2GW33ID7PFMT7BNNOYO/
Code of Conduct: http://python.org/psf/codeofconduct/

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GZM4BXAMYWVRUQ55ZVKVHTWGRZ5UYZQG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to