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

4
  • 2
    Refer to any. Commented Sep 12, 2022 at 6:59
  • 1
    O(n); it checks every element passed as argument Commented Sep 12, 2022 at 7:00
  • Does this answer your question? How do Python's any and all functions work? Commented Sep 12, 2022 at 7:01
  • None of those mentioned computational cost, hence why this is in a separate question. Commented Sep 12, 2022 at 7:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.