Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of any() function

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 
like image 818
Ekeyme Mo Avatar asked Aug 10 '16 09:08

Ekeyme Mo


People also ask

What is the opposite of any in Python?

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.

What are any () and 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.

What's the opposite of a function?

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.

What does any () do in Python?

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.


2 Answers

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) 
like image 166
Jack Aidley Avatar answered Sep 20 '22 08:09

Jack Aidley


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) 
like image 26
deceze Avatar answered Sep 21 '22 08:09

deceze