Python all() method

The all() method checks whether all the elements in an iterable are truthy values or not. It returns True if all elements in the given iterable are nonzero or True. Else, it returns False.

all() Syntax:

all(iterable)

Parameters:

iterable: A list, tuple, string, set, or dictionary object.

Return type:

Returns a boolean value True or False.

The following example demonstrates the all() method on list objects.

Example: all()
lst = []
print(all(lst)) # Returns True for empty list

nums = [1, 2, 3, -4, -5]
print(all(nums)) # Returns True

nums = [0, 1, 2, 3]
print(all(numbers)) # Returns False because of 0

data = [1, 'Phone', 12.5, 5, False]
print(all(data)) # Returns False because of False
Output
True
True
False
False

The following example demonstrates the all() method with strings.

Example: all() with String
print(all('Python'))
print(all(''))
print(all('False'))
Output
True
True
True

The following example demonstrates the all() method with dictionaries.

Example:
numsdict = {1:'One', 2:'Two', 3:'Three'}
print(all(numsdict)) # Returns True

numsdict = {0:'zero', 1:'One', 2:'Two', 3:'Three'}
print(all(numsdict)) # Returns False for 0
Output
True
False