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

ParameterDescription
xAn 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}')
Run Code Copy

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 Code Copy

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}')
Run Code Copy

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 Code Copy

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.

Frequently Asked Questions

1. What does all() function do in Python?

Answer:

all() function takes an iterable as argument and returns True, only if all the elements in the iterable evaluate to true. The function returns False, even if there is a single element that evaluates to False in the given iterable.

all([True, True, True]) #returns True
all([True, False, True]) #returns False
2. What is the difference between all() and any() functions in Python?

Answer:

all() function returns True, only if all the elements in the given iterable evaluate to True. Whereas any() function returns True if there is at least one element that is True in the given iterable.

abs(-8)

Related Tutorials

Code copied to clipboard successfully 👍