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)
Parameter | Condition | Description |
iterable | Required | An 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
andFalse
. - Zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- Empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
Basic Examples
# Check if all items in a list are True
L = [1, 1, 1]
print(all(L)) # Prints True
L = [0, 1, 1]
print(all(L)) # Prints False
Here are some scenarios where all()
returns False.
L = [True, 0, 1]
print(all(L)) # Prints False
T = ('', 'red', 'green')
print(all(T)) # Prints False
S = {0j, 3+4j}
print(all(S)) # Prints False
all() on a Dictionary
When you use all()
function on a dictionary, it checks if all the keys are true, not the values.
D1 = {0: 'Zero', 1: 'One', 2: 'Two'}
print(all(D1)) # Prints False
D2 = {'Zero': 0, 'One': 1, 'Two': 2}
print(all(D2)) # Prints True
all() on Empty Iterable
If the iterable is empty, the function returns True.
# empty iterable
L = []
print(all(L)) # Prints True
# iterable with empty items
L = [[], []]
print(all(L)) # Prints False