This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add if condition for 'for loop'
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: chienpingtsung, ronaldoussoren
Priority: normal Keywords:

Created on 2020-10-05 10:33 by chienpingtsung, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg378015 - (view) Author: (chienpingtsung) Date: 2020-10-05 10:33
'for loop' always been used as follow code:

arr = [...]
for e in arr:
    if not condition(e):
        continue
    ...

What if be written just like 'list expression', would it be more explicit?

for e in arr if condition(e):
    pass
msg378020 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-10-05 12:28
This is a language change that has been discussed on the python-ideas list a number of times.

In my opinion adding this would not necessarily be an improvement, especially when names and expressions get longer. You'd also not win a lot compared to your initial pattern, especially because the "continue" statement can be placed on the same line as the if statement itself (for short enough conditions).

This is something that should be discussed on python-ideas, but please investigate what's been discussed before before you do so.
msg378022 - (view) Author: (chienpingtsung) Date: 2020-10-05 13:00
Got it, thanks for your reply~
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86108
2020-10-05 13:00:04chienpingtsungsetstatus: open -> closed
resolution: not a bug
messages: + msg378022

stage: resolved
2020-10-05 12:28:12ronaldoussorensetnosy: + ronaldoussoren
messages: + msg378020
2020-10-05 10:33:27chienpingtsungcreate