Python any() function is one of the built-in functions. It takes iterable as an argument and returns True if any of the element in the iterable is True. If iterable is empty, then it returns False.
Python any() function
Python any() function is a utility method and shortcut to below function.
1 2 3 4 5 6 7 |
def any(iterable): for element in iterable: if element: return True return False |
Let’s look at some of the examples of python any() function.
Python any() example with boolean
1 2 3 4 5 6 7 8 |
# iterable has at least one True list_bools = [True, True, True] print(any(list_bools)) # iterable none of the elements are True list_bools = [False, False] print(any(list_bools)) |
Output:
1 2 3 4 |
True False |
Python any() with empty iterable
1 2 3 4 5 |
# iterable is empty list_bools = [] print(any(list_bools)) # False |
Python any() with list of strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# iterable elements are True string (at least one) list_strs = ['True', 'false'] print(any(list_strs)) # iterable any elements is true string with different case list_strs = ['fff', 'true'] print(any(list_strs)) # iterable any elements are not true string list_strs = ['abc', 'def'] print(any(list_strs)) # iterable all elements are empty string list_strs = ['', ''] print(any(list_strs)) |
Output:
1 2 3 4 5 6 |
True True True False |
When we want an object boolean value, python looks for __bool__
function in the object.
If __bool__
function is not defined, then len()
function is called if it’s defined. The object boolean value is considered as True if len()
output is non-zero.
If a class defines neither __len__()
nor __bool__()
functions, all its instances are considered True.
Python 3 uses __bool__
function to check object boolean value, if you are using python 2.x then you have to implement __nonzero__
function.
Python any() with custom objects
Let’s test above explanation with a custom class. We will create a custom Employee class and use its objects in the list and call any()
function on it.
1 2 3 4 5 6 7 8 9 10 |
class Employee: name = "" def __init__(self, n): self.name = n list_objs = [Employee("Pankaj"), Employee("Lisa")] print(any(list_objs)) list_objs = [Employee("A"), Employee("D")] print(any(list_objs)) |
Output:
1 2 3 4 |
True True |
Since our Employee class doesn’t have __len__() and __bool__() function defined, it’s boolean value is True.
Let’s go ahead and define __len__() function for the Employee class as below.
1 2 3 4 5 |
def __len__(self): print('len function called') return len(self.name) |
Now the output of earlier code snippets will be:
1 2 3 4 5 6 |
len function called True len function called True |
Notice that len()
function is getting called when any() is used with the list of Employee objects. Since the first object in the iterable returned True, other elements are not required to be evaluated for the boolean value.
Now let’s define __bool__ function for the Employee class and see what happens with the above code.
1 2 3 4 5 6 7 8 |
def __bool__(self): print('bool function called') if len(self.name) > 3: return True else: return False |
Output:
1 2 3 4 5 6 7 |
bool function called True bool function called bool function called False |
It’s clear from the output that if __bool__
function is defined, then it’s used for getting the python object boolean value.
Notice that second list any() function output is False and boolean value is retrieved for all the objects in the list.
That’s all for python any() function examples.
Reference: Official Documentation