The Python built-in function any(iterable)
can help to quickly check if any bool(element)
is True
in a iterable type.
>>> l = [None, False, 0] >>> any(l) False >>> l = [None, 1, 0] >>> any(l) True
But is there an elegant way or function in Python that could achieve the opposite effect of any(iterable)
? That is, if any bool(element) is False
then return True
, like the following example:
>>> l = [True, False, True] >>> any_false(l) >>> True
Python any() is an inbuilt function that returns True if any element of an iterable is True otherwise returns False. If the iterable object is empty, the any() returns False. The any() function is the opposite of all() function.
The Python any() and all() functions evaluate the items in a list to see which are true. The any() method returns true if any of the list items are true, and the all() function returns true if all the list items are true.
An inverse function is a function that undoes the action of the another function. A function g is the inverse of a function f if whenever y=f(x) then x=g(y). In other words, applying f and then g is the same thing as doing nothing. We can write this in terms of the composition of f and g as g(f(x))=x.
Python any() Function The any() function returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.
There is also the all
function which does the opposite of what you want, it returns True
if all are True
and False
if any are False
. Therefore you can just do:
not all(l)
Write a generator expression which tests your custom condition. You're not bound to only the default truthiness test:
any(not i for i in l)
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