Python all(): How It Works and When to Use It
Understand Python's all() function: syntax, truthiness, edge cases, and practical examples for validation and condition checks.

Python's built-in all() function is a concise way to check whether every element in an iterable is truthy. It is a common tool for validation and condition checks. Understanding its exact behavior, including edge cases like empty iterables and generator consumption, is essential for writing correct code.
What all() Does and When to Use It
all(iterable) returns True if every element in the iterable evaluates to True in a boolean context, and False otherwise. Its boolean result matches the result of chaining the elements with and. For example, all([a, b, c]) returns the same boolean result as bool(a and b and c), but without requiring you to write out the full expression.
The function is particularly useful when you need to verify that a collection of values meets a condition. Common scenarios include:
- Checking that all items in a list are truthy or non-empty.
- Validating that every field in a form is filled.
- Ensuring all elements in a sequence satisfy a predicate.
- Confirming that a set of flags are all enabled.
Because all() is concise, it is often more readable than a manual loop for these checks.
Syntax and Basic Behavior
The syntax is straightforward:
all(iterable)The argument must be an iterable. It can be a list, tuple, set, dictionary, generator, or any object that supports iteration. The function iterates over the elements and evaluates each one's truthiness. As soon as it finds a falsy value, it returns False without examining the rest. If the iterable is empty, it returns True.
For a dictionary, iteration is over keys unless you pass .values().
Here is a minimal example:
print(all([1, 2, 3])) # True
print(all([1, 0, 3])) # False
print(all([])) # True
print(all([True, True])) # TrueThe empty iterable case often surprises developers. The behavior is consistent with the mathematical convention that a universal quantification over an empty set is true. If you need a different default, you must handle it explicitly.
How Truthiness Works Inside all()
all() relies on Python's truthiness rules. Every object in Python has a boolean value: False, 0, None, empty collections, and objects whose __bool__() returns False or whose __len__() returns 0 are considered falsy. Everything else is truthy.
This means all() works with any data type, not just booleans. For example:
print(all([1, 'hello', 3.14])) # True
print(all([0, 'hello', 3.14])) # False
print(all(['', 'hello'])) # False
print(all([None, 1])) # FalseWhen you need to check a specific condition rather than raw truthiness, combine all() with a generator expression:
numbers = [2, 4, 6, 8]
print(all(n % 2 == 0 for n in numbers)) # TrueThe generator expression evaluates each element against the condition, and all() consumes the resulting booleans. This pattern is common and avoids building an intermediate list.
Common Use Cases for all()
A frequent use case is validating that all elements in a collection satisfy a constraint. For instance, checking that all strings in a list are truthy (non-empty):
strings = ['apple', 'banana', 'cherry']
if all(strings):
print('All strings are truthy')Another pattern is verifying that all values in a dictionary are above a threshold:
scores = {'math': 85, 'science': 92, 'history': 88}
if all(score >= 80 for score in scores.values()):
print('All scores are at least 80')all() also works with custom objects that define __bool__() or __len__(). If you have a class representing a transaction that is valid only when certain fields are set, you can use all() to check a list of such objects.
Edge Cases and Pitfalls
Several edge cases can lead to subtle bugs if you overlook them.
Empty Iterable
As noted, all([]) returns True. This is often unexpected. If your logic depends on the iterable having at least one element, you must check that separately:
items = []
if items and all(item.is_valid() for item in items):
# This block will not run because items is emptyGenerators Are Consumed
If you pass a generator to all(), it will consume the generator. After the call, the generator is exhausted. This is fine if you only need the result once, but it can cause issues if you plan to reuse the generator later.
gen = (x for x in range(1, 6))
print(all(gen)) # True
print(list(gen)) # [] because the generator is exhaustedNon-Iterable Arguments
Passing a non-iterable raises a TypeError. The error message is usually clear: 'int' object is not iterable. Ensure the argument is always an iterable.
Truthiness of Objects
Remember that all() checks truthiness, not equality to True. An object with a custom __bool__() method may behave unexpectedly. For example, a numeric array from NumPy may raise an error if its truth value is ambiguous. In such cases, use an explicit condition.
Performance and Short-Circuiting
all() short-circuits: it stops evaluating as soon as it encounters a falsy value. This can save time when the iterable is large and the first falsy element appears early. The function does not build an intermediate list unless you use a list comprehension; using a generator expression avoids extra memory allocation.
For example, the equivalent manual loop is:
# Manual loop
def all_manual(iterable):
for item in iterable:
if not item:
return False
return TrueIn CPython, all() is implemented in C, so for simple truthiness checks it can be faster than a hand-written Python loop. With a generator expression, the per-item Python call overhead is often more significant than the all() traversal itself.
Memory usage is also modest. all() does not build a result list, and when passed a generator it processes items one at a time.
all() vs. any() vs. Manual Loops
all() is the counterpart to any(), which returns True if at least one element is truthy. Choosing between them depends on the logical requirement:
| Function | Returns True when | Short-circuits on |
|---|---|---|
all() | Every element is truthy | First falsy element |
any() | At least one element is truthy | First truthy element |
A manual loop gives you explicit control when you need to inspect the failing item, access the index, or perform side effects around each element. For a simple truthiness check, though, all() is more concise and less error-prone.
Consider using all() when:
- The condition is a simple truthiness check or a generator expression.
- You want to avoid boilerplate loop code.
- The iterable is large and short-circuiting is beneficial.
Use a manual loop when:
- You need to perform additional operations on each element.
- The logic is too complex to express in a generator expression.
- You need to handle the empty-iterable case with custom logic.
In practice, all() is a valuable tool for writing clear and efficient validation code. Understanding its behavior, especially the empty-iterable rule and generator consumption, helps you avoid subtle bugs.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.