Python all() Function

Determines whether all items in an iterable are True

Usage

The all() function returns True if all items in an iterable are True. Otherwise, it returns False.

If the iterable is empty, the function returns True.

Syntax

all(iterable)

Python all() function parameters
ParameterConditionDescription
iterableRequiredAn iterable of type (list, string, tuple, set, dictionary etc.)

Falsy Values

In Python, all the following values are considered False.

  • Constants defined to be false: None and False.
  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: '', (), [], {}, set(), range(0)

Basic Example

Example: Check if all items in a list are True

# all true
L = [1, 1, 1]
x = all(L)
print(x)    # True
# one false
L = [0, 1, 1]
x = all(L)
print(x)    # False

More Examples

Example: Check if all items in a list are True

L = [True, 0, 1]
x = all(L)
print(x)    # False

Example: Check if all items in a tuple are True

T = ('', 'red', 'green')
x = all(T)
print(x)    # False

Example: Check if all items in a set are True

S = {0j, 3+4j}
x = all(S)
print(x)    # False

Example: Check if all items in a dictionary are True

D = {0: 'Zero', 1: 'One', 2: 'Two'}
x = all(D)
print(x)    # False

When you use all() function on a dictionary, it checks if all the keys are true, not the values.

D = {'Zero': 0, 'One': 1, 'Two': 2}
x = all(D)
print(x)    # True

all() on Empty Iterable

If the iterable is empty, the function returns True.

Example:

# empty iterable
L = []
x = all(L)
print(x)    # True
# iterable with empty items
L = [[], []]
x = all(L)
print(x)    # False