all() Builtin Function
Python - all()
Python all() builtin function is used to check if all the items in a given iterable are True.
all() returns a boolean value of True if all the items in the given iterable are True, else it return False.
In this tutorial, you will learn the syntax of abs() function, and then its usage with the help of example programs.
Syntax
The syntax of all()
function is
all(x)
where
Parameter | Description |
---|---|
x | An iterable like list, tuple, etc. |
Examples
1. 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}')
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}')
Output
x : [True, False, True, True]
all(x) : False
2. 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}')
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}')
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.