In this python tutorials, We will learn how to use all()
and any()
function in our python project with example.The all(iterable)
and any(iterable)
are built-in functions in Python, So You don’t need to include any external libs into your python project.
The any()
function behaves like “OR” operator Whereas all()
function behaves like “AND” operator.
Python any() function
The any()
function returns True if any item in an iterable are true, otherwise it returns False. However, if the iterable object is empty, the any ()
function will return False.
Syntax
any(iterable)
The iterable object can be a list, tuple or dictionary.
Let’s take a simple example of python any function –
print(any([4 == 2, 2 == 2])) print(any([False, False])) print(any([True, False, False]))
The Output –
True
False
True
Note – When you passed dictionary as a parameters into
any()
function, it checks whether any of the keys evaluate to True, not the values:
dict = {True : False, False: False} print(any(dict))
The outputs:
True
The output should be False, if its evaluated dict values.
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 all returns True.
Syntax
all(iterable)
The iterable object can be list, tuple or dictionary.
Let’s take a simple example –
print(any([4 == 2, 2 == 2])) print(any([True, True])) print(any([True, False, False]))
The result would be –
False
True
False