all() Builtin Function

Python – all()

Python all() builtin function returns a boolean value of True if all the items in the given iterable are True, else it return False.

Syntax

The syntax of all() function is

all(x)

where x is an iterable like list, tuple, etc.

Examples

Check if all items in list are True

In the following program, we take a list of items x where all items are True, and find out the output of all() with x passed as argument to it.

Python Program

x = [True, True, True, True]
output = all(x)
print(f'x      : {x}')
print(f'all(x) : {output}')
Run

Output

x      : [True, True, True, True]
all(x) : True

Now, let us change some of the items in x to False, and find out the output.

Python Program

x = [True, False, True, True]
output = all(x)
print(f'x      : {x}')
print(f'all(x) : {output}')
Run

Output

x      : [True, False, True, True]
all(x) : False

Check if all the strings are non-empty in a list

An empty string is False, and a non-empty string is True in terms of logical values. So, if we need to check if all the items in given list are non-empty strings, pass the list of strings to all() function.

Python Program

x = ["apple", "banana", "cherry"]
output = all(x)
print(f'x      : {x}')
print(f'all(x) : {output}')
Run

Output

x      : ['apple', 'banana', 'cherry']
all(x) : True

Now, let us take some empty strings in the list and find out the result.

Python Program

x = ["apple", "", "cherry"]
output = all(x)
print(f'x      : {x}')
print(f'all(x) : {output}')
Run

Output

x      : ['apple', '', 'cherry']
all(x) : False

Summary

In this tutorial of Python Examples, we learned the syntax of all() function, and how to find if all the items in the given iterator are True using all() with examples.