What is the computational cost of using any() or not any()?
The any() built-in checks if any element is True in an iterable. I would suppose it needs to check all the elements it is applied to, so I would assume the cost to be O(n).
What is the computational cost of using any() or not any()?
The any() built-in checks if any element is True in an iterable. I would suppose it needs to check all the elements it is applied to, so I would assume the cost to be O(n).
Looking to the documentation it says the function any is equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False
So we can assume the computational cost is, as you state, O(n).