Python all() Function
The all()
method returns True
if all the elements in the iterable are true or if the iterable is empty. If not, it returns False
.
Example:
# Check if all elements are true
is_available = [True, "True", 1]
output = all(is_available)
print(output)
Output
True
Syntax
The Syntax of the all()
function is:
all(iterable)
Here, iterable
could be a list, tuple, dictionary, etc.
Parameters
The all()
function takes single parameter.
- iterable - the iterable, which contains a series of elements. The iterable could be of type list, tuple, dictionary, etc.
Return Value
The all()
function returns:
- Returns
True
if all the elements in the iterable are true - Returns
False
if at least one of the elements in the iterable is false - Returns
True
if the iterable is empty
Condition | Return Value |
---|---|
All the elements are true | True |
All the elements are false | False |
One of the elements is true, and others are false) | False |
One of the elements is false, and others are true | False |
Empty Iterable | True |
Difference between all()
and any()
Methods
The all()
and any()
methods are similar to Python's Logical AND
and OR
operators.
The all()
method returns True
when at least all the elements in the iterable are true, whereas the any()
method returns True
when at least one of the elements in the iterable is true.
Scenario | any() | all() |
---|---|---|
All Truthy values | True | True |
All Falsy values | False | False |
One Truthy value (all others are Falsy) | True | False |
One Falsy value (all others are Truthy) | True | False |
Empty Iterable | False | True |
Example 1: Using all() method with a list in Python
The all()
method in the below example returns True
if all the elements in the iterable are true or if it's an empty list.
# All the elements in the list are true
lst = [1, 2, 3, 4, 5]
print(all(lst))
# All the elements in the list are false
lst = [0, 0, False, "False"]
print(all(lst))
# One of the elements in the list is false
lst = [1, 2, 3, 4, False]
print(all(lst))
# Only one element in the list is true
lst = [0, 0, "False", False, 100]
print(all(lst))
# Empty List
lst = []
print(all(lst))
Output
True
False
False
False
True
Example 2: Using all() method with Dictionary
In the case of dictionaries, the all()
method returns True
if all the keys in the dictionary are true or if it's an empty dictionary.
Note: It only considers the dictionary keys, not the values.
# All the elements in the dictionary are true
item = {1: "Python", 2: "Java", 3: "C#"}
print(all(item))
# All the elements in the dictionary are false
item = {0: "Python", False: "C#"}
print(all(item))
# One of the elements in the dictionary is false
item = {0: "Python", 1: "Java", 2: "C#"}
print(all(item))
# Empty dictionary
item = {}
print(all(item))
Output
True
False
False
True
Example 3: Using all() method with Tuple
The all()
method in the below example returns True
if all the elements in the tuple are true or if it's an empty tuple.
# All the elements in the tuple are true
item = (1, 2, 3, 4, 5)
print(all(item))
# All the elements in the tuple are false
item = (0, 0, False)
print(all(item))
# One of the elements in the tuple is false
item = (1, 2, 3, 4, False)
print(all(item))
# Only one element in the tuple is true
item = (0, 0, "False", False, 100)
print(all(item))
# Empty tuple
item = ()
print(all(item))
Output
True
False
False
False
True
Example 4: Using all() method with Strings
In the case of a string, even if the string value is "0", the all()
method returns True
since it's a valid string value and evaluates to Truthy.
# Non-Empty String
text = "Welcome to Python Tutorial"
print(all(text))
# Empty String
text = ""
print(all(text))
# Text with 0 is true as its string
text ="0"
print(all(text))
Output
True
True
True
Reference: Python Official Docs