Consider the following python instruction
any([condition(element) for element in some_list])
Is it optimised to stop once condition(element)
is True
, or any
is applied after the whole list of boolean values is built?
If case the the whole list is created, is there a way to avoid it, apart from coding an explicit loop?
Let me clarify the question, since someone misunderstood it.
Scenario 1. condition(element)
is evaluated for each element of some_list
and list of boolean values (containing True or False for corresponding element of some_list) is created. Only after that any is applied to the created list of booleans.
Scenario 2. Instead of evaluating condition(element)
for each element, the lookup stops immediately after condition(element)
holds for certain element of some_list
Is Python optimised to follow Scenario 2 rather than Scenario 1?