Python all() Builtin Function

Python all() function takes an iterable as argument and returns True if all the elements in the iterable are True.

all() function returns True if the iterable is empty.

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

Syntax

The syntax to check if all the elements of an iterable are True is

result = all(iterable)

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

The definition of all() function in Python is

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

So, if any of the element is False, then all() function returns False. Or in other words, all() returns True only if all the elements in the iterable are True.

ADVERTISEMENT

Python all() Function with List as Argument

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

Python Program

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

Output

all(myList) returns True

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

Python Program

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

Output

all(myList) returns False

Python all() Function with Tuple as Argument

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

Python Program

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

Output

all(myTuple) returns True

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

Python Program

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

Output

all(myTuple) returns False

Conclusion

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