In Python, I often find myself implementing the same pattern: count the number of "valid" iterations while processing within a loop, where an "invalid" iteration is skipped over with a continue statement. I use the continue statement instead of if-else
blocks to improve readability. Essentially, I do the following:
count = 0
for item in collection:
do_something_1(item)
if not evaluate_some_condition(item):
continue
count += 1
do_something_2(item)
return count
There are several nifty tricks one can use to implement similar patterns in a Pythonic manner. For example, enumerate
, continue
, break
, for-else
, and while-else
come to mind. I am looking for a Pythonic construct to implement the scenario described above.
This works (below) but would require the evaluate_some_condition
function be executed twice for every element, which can sometimes be unacceptable (it is also less readable in my opinion):
count = sum(1 for item in collection if not evaluate_some_condition(item))
for item in collection:
do_something_1(item)
if not evaluate_some_condition(item):
continue
do_something_2(item)
return count
Some construct like the below would be ideal:
for count, item in uninterrupted_enumerate(collection):
do_something_1(item)
if not evaluate_some_condition(item):
continue
do_something_2(item)
return count
Any ideas of a built-in Python feature, third-party feature, or future plans to include such a feature?