Aggregate validations
Validate a collection of predicates before proceeding.
Python · Function
Returns True when every element in an iterable is truthy, and also for an empty iterable. It short-circuits at the first false element.
all consumes an iterable and tests the truth value of each element from left to right. It stops as soon as a false value appears, and returns True when no element is false, including when the iterable has no elements.
all(iterable, /)| Name | Type | Description |
|---|---|---|
iterable | iterable | An iterable whose elements are tested for truth. |
True if every element of iterable is true, or if iterable is empty; otherwise False.
healthy = [200, 200, 204]
if all(status < 400 for status in healthy):
print("all checks passed")Checks each status lazily and prints only when every status is below 400.
Validate a collection of predicates before proceeding.
Check streamed or generated values without materializing them.
Confirm every status, permission, or feature flag meets a condition.
all takes one iterable and returns True only when every element is truthy. It also returns True for an empty iterable.
Follow-up: What happens with a generator? all consumes it as it iterates, stopping when it encounters the first false value.
all accepts any iterable, not only a list. A generator expression is useful when values can be produced lazily, because evaluation can stop early.
Follow-up: Why can this reduce work? Values after the first false element are never requested.
The documented equivalent loops over the iterable and immediately returns False when an element is false; after the loop, it returns True. This explains both short-circuiting and the empty-iterable result.
Follow-up: How would you require at least one item too? Check that the iterable is non-empty separately, because all alone treats emptiness as success.
anyboolfilter