0

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).

DarknessPlusPlus
  • 543
  • 1
  • 5
  • 18

1 Answers1

2

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).

metc
  • 63
  • 6