Python any() Builtin Function

Python any() function takes an iterable as argument and returns True if at least one of the elements in the iterable is True.

Python any() function can be used during decision making situations, where you have a list or iterable with boolean values and the condition you need is any one of those elements to be True or not.

any() function returns False if the iterable is empty.

In this tutorial, we will take different iterable objects and pass them as argument to any() function, and observe the return value.

Syntax

The syntax to check if any of the elements in an iterable is True is

result = any(iterable)

The iterable could be list, set, tuple, dictionary, or any other object that implement __iter__() and __next__() methods.

The definition of any() function in Python is

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
Try Online

So, if any of the element is True, then any() function returns True.

ADVERTISEMENT

Python any() Function with List as Argument

In the following example, we will take List of boolean values and call any() function with this list passed as argument to the function.

Python Program

myList = [True, False, True, True]
result = any(myList)
print(f'any(myList) returns {result}')
Try Online

Output

any(myList) returns True

Now, let us take all values as False in the List and observe the result.

Python Program

myList = [False, False, False, False]
result = any(myList)
print(f'any(myList) returns {result}')
Try Online

Output

any(myList) returns False

Python any() Function with Tuple as Argument

In the following example, we will take Tuple of boolean values and call any() function with this list passed as argument to the function.

Python Program

myTuple = (True, True, False, False)
result = any(myTuple)
print(f'any(myTuple) returns {result}')
Try Online

Output

any(myTuple) returns True

Now, let us take all values as False in the Tuple and observe the result.

Python Program

myTuple = (False, False, False)
result = any(myTuple)
print(f'any(myTuple) returns {result}')
Try Online

Output

any(myTuple) returns False

Conclusion

In this Python Tutorial, we learned about any() builtin function in Python, with the help of example programs.