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.
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
# 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))
Python any() with empty iterable
# iterable is empty list_bools =  print(any(list_bools)) # False
Python any() with list of strings
# 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))
True True True False
When we want an object boolean value, python looks for
__bool__ function in the object.
__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
__bool__() functions, all its instances are considered True.
__bool__function to check object boolean value, if you are using python 2.x then you have to implement
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.
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))
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.
def __len__(self): print('len function called') return len(self.name)
Now the output of earlier code snippets will be:
len function called True len function called True
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.
def __bool__(self): print('bool function called') if len(self.name) > 3: return True else: return False
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