I am pretty sure there is a common idiom, but I couldn't find it with Google Search...
Here is what I want to do (in Java):
// Applies the predicate to all elements of the iterable, and returns // true if all evaluated to true, otherwise false boolean allTrue = Iterables.all(someIterable, somePredicate);
How is this done "Pythonic" in Python?
Also would be great if I can get answer for this as well:
// Returns true if any of the elements return true for the predicate boolean anyTrue = Iterables.any(someIterable, somePredicate);
Python all() Function The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.
We can use all() , to perform this particular task. In this, we feed the condition and the validation with all the elements is checked by all() internally. This function can also be used to code solution of this problem. In this, we just need to process the loop till a condition is met and increment the counter.
The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True, else it returns False. It also returns True if the iterable object is empty.
sum(data) represents the addition of 1 and 0 with respective values of True(1) and False(0) in a list. In the case of all False sum is 0, and in the case of all True sum is equal to the length of the list. any sum value other than 0 & 1 means not all is False or True.
Do you mean something like:
allTrue = all(somePredicate(elem) for elem in someIterable) anyTrue = any(somePredicate(elem) for elem in someIterable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With