The all() function in Python is a built-in function used to check whether all elements in an iterable evaluate to True. It returns True when every element is truthy or when the iterable is empty; otherwise, it returns False.
Example:
values = [True, True, False]
result = all(values)
print(result)
Output
False
Explanation:
- The list contains three True values.
- Since all elements evaluate to True, all() returns True.
Syntax
all ( iterable )
Explanation:
- iterable is any iterable object such as a list, tuple, set, string, dictionary, or generator.
- Returns True if all elements evaluate to True.
- Returns False when any element evaluates to False.
With Strings
A string is an iterable, so all() check each character.
text = "Python"
print(all(text))
Output
True
Explanation:
- Each character is a non-empty string.
- Non-empty strings evaluate to True.
- Therefore, all() returns True.
With Conditions
A common use of all() is checking whether every element satisfies a condition.
Example 1:
numbers = [10, 20, 30, 40]
result = all(num > 5 for num in numbers)
print(result)
Output
True
Explanation:
- Expression checks whether each number is greater than 5.
- Every number satisfies the condition.
- Hence, all() returns True.
Example 2:
numbers = [10, 20, 3, 40]
result = all(num > 5 for num in numbers)
print(result)
Output
False
Explanation:
- 10 and 20 are greater than 5.
- 3 is not greater than 5.
- Therefore, all() returns False.
With Empty Iterable
When an empty iterable is passed to all(), it returns True.
values = []
print(all(values))
Output
True
Explanation:
- There is no element that evaluates to False.
- Therefore, all() returns True for an empty iterable.
With Dictionary
When a dictionary is passed directly to all(), its keys are evaluated.
Example 1:
data = {
1: "Python",
2: "Java",
3: "C++"
}
print(all(data))
Output
True
Explanation:
- all(data) iterates over the dictionary keys.
- Keys 1, 2, and 3 are all truthy.
- Therefore, the result is True.
Example 2:
data = {
0: "Python",
2: "Java"
}
print(all(data))
Output
False
Explanation:
- Dictionary iteration checks the keys.
- 0 is a falsy value.
- Therefore, all() returns False.
Short-Circuiting in all()
all() function uses short-circuit evaluation. It stops checking elements as soon as a falsy value is found.
def check(value):
print("Checking:", value)
return value
result = all(check(x) for x in [1, 2, 0, 4])
print("Result:", result)
Output
Checking: 1 Checking: 2 Checking: 0 Result: False
Explanation:
- all() checks 1 and 2.
- 0 evaluates to False.
- all() immediately stops execution.
- 4 is not checked.
With Generator Expression
Generator expressions can be used with all() to check conditions without first creating a separate list.
numbers = range(1, 6)
result = all(num % 2 == 0 for num in numbers)
print(result)
Output
False
Explanation:
- The expression checks whether each number is even.
- 1 is not even.
- all() immediately returns False.
Practical Example
Checking Weather a List is Sorted
numbers = [1, 2, 3, 4, 5]
result = all(
numbers[i] <= numbers[i + 1]
for i in range(len(numbers) - 1)
)
print(result)
Output
True
Explanation:
- Each element is compared with the next element.
- Every element is less than or equal to the following element.
- Therefore, the list is sorted in ascending order.
- all() returns True.