-3

Given x = [False, False, True]

How do I evaluate in Python if any of the values of x is True without an explicit loop?

Please note that my example above is oversimplified, and the C++ std::any_of function takes a lambda for the unary callback.

If the collection above is an object with a boolean, How do I do acomplish this in Python without having to create a new collection, but just evaluating each element until one of the callback calls return True?

Daniel V.
  • 183
  • 2
  • 6
  • 1
    @JohnDing - I didn't downvote, and don't claim to be able to read minds of downvoters. But questions that can be easily answered by simply searching through readily available documentation (in this case, of the Python library) tend to attract downvotes, since they demonstrate that the OP has not applied any effort of their own. – Peter Mar 04 '20 at 07:28
  • I should probably had asked this to my search engine in terms of the description instead of the name of C++ function. I found https://stackoverflow.com/questions/5217489/check-if-a-predicate-evaluates-true-for-all-elements-in-an-iterable-in-python; but I didn't know collections/containers are called "an iterable" so I just wanted whatever std::any_of is doing in Python. – Daniel V. Mar 04 '20 at 16:49

2 Answers2

2

The any() built-in function works like that. So

any(x) is True

Check out https://docs.python.org/3/library/functions.html#any

Jason Chen
  • 194
  • 1
  • 5
2

Use any(iterable):

x = [False, False, True]
any(x)  # True.
Flux
  • 9,805
  • 5
  • 46
  • 92