The all() function returns True if all items in an iterable are True. Otherwise, it returns False.
all() Syntax
all(iterable)all() Parameters
Python all() function parameters:
| Parameter | Condition | Description |
|---|---|---|
| iterable | Required | An iterable of type (list, string, tuple, set, dictionary etc.) |
all() Return Value
Python all() function returns:
True: if all elements in an iterable are trueFalse: 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:
NoneandFalse. - 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 returnFalse, or the__len__()method returns0when__bool__()is not defined.
Examples
Some examples of how to use all() function.
all() function on List
my_list = [1, 1, 1]
print(all(my_list)) # Output: True
my_list = [0, 1, 1]
print(all(my_list)) # Output: Falseoutput
True
FalseOther 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: Falseoutput
False
False
Falseall() function on String
s = "This is good"
print(all(s)) # Output: True
s = '000'
print(all(s)) # Output: True
s = ''
print(all(s)) # Output: Trueoutput
True
True
Trueall() 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: Trueoutput
False
Trueall() function on Empty Iterable
The function all() can be used on iterables.
In particular, if the iterable is empty, the function returns True.
my_list = []
print(all(my_list)) # Output: True
my_list = [[], []]
print(all(my_list)) # Output: Falseoutput
True
False