Skip to main content

Python all() Function

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

note

If the iterable is empty, the function returns True.

all() Syntax

all(iterable)

all() Parameters

Python all() function parameters:

ParameterConditionDescription
iterableRequiredAn iterable of type (list, string, tuple, set, dictionary etc.)

all() Return Value

Python all() function returns:

  • True: if all elements in an iterable are true
  • False: if any element in an iterable is false

Falsy Values in Python

Falsy values are those that evaluate to False in a boolean context:

  • Constants defined to be false: None and False.
  • Zero of any numeric type: Integer (0), Float (0.0), Complex (0j), Decimal(0), Fraction(0, 1).
  • Empty sequences and collections: Lists ([]), Tuples (()), Dictionaries ({}), Sets (set()), Strings (""), range(0)
  • Any custom object for which the __bool__() method is defined to return False, or the __len__() method returns 0 when __bool__() is not defined.

Examples

Some examples of how to use all() function.

all() function on List

# Check if all item in a list is True
my_list = [1, 1, 1]
print(all(my_list)) # Output: True

my_list = [0, 1, 1]
print(all(my_list)) # Output: False

output

True
False

Other examples:

my_list = [True, 0, 1]
print(all(my_list)) # Output: False

my_listT = ('', 'red', 'green')
print(all(my_list)) # Output: False

my_list = {0j, 3+4j}
print(all(my_list)) # Output: False

output

False
False
False

all() function on String

s = "This is good"
print(all(s)) # Output: True

# 0 is False
# '0' is True
s = '000'
print(all(s)) # Output: True

s = ''
print(all(s)) # Output: True

output

True
True
True

all() function on Dictionary

The function all() can be used on dictionaries.

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

dict1 = {0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three'}
print(all(dict1)) # Output: False

dict2 = {'Zero': 0, 'One': 1, 'Two': 2, 'Three': 3}
print(all(dict2)) # Output: True

output

False
True

all() function on Empty Iterable

The function all() can be used on iterables. In particular, if the iterable is empty, the function returns True.

# empty iterable
my_list = []
print(all(my_list)) # Output: True

# iterable with empty items
my_list = [[], []]
print(all(my_list)) # Output: False

output

True
False