all()

Short-circuit "is every item truthy?" over any iterable. Empty is True by convention.

Built-in functionPython 2.5+Live demo
Common call
if all(x.is_valid for x in rows):
Returns
True or False
Replaces
the multi-line for-loop-with-flag pattern
Watch out
empty iterable → True (vacuously)
all(iterableiterableAny iterable — list, tuple, generator, set, dict (keys), file lines. Items are evaluated for truthiness the same way `if item:` does.type: iterable · required)
bool

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated items
Output
all(['1', '2', '3'])
True

all walks the iterable and stops the moment it sees a falsy item — a generator input would not be consumed past that point. Empty iterables return True by convention: there is no falsy item, so "are all truthy?" is vacuously true. Falsy values in Python: 0, 0.0, "", [], {}, None, False.

Parameters

NameTypeRequiredDescription
iterableiterableyesAny iterable — list, tuple, generator, set, dict (keys), file lines. Items are evaluated for truthiness the same way `if item:` does.

Return value

boolTrue if every item is truthy OR the iterable is empty. False as soon as one falsy item is seen.

Common patterns

Validate every row
Generator expression + all — reads like the English question.
if all(row.is_valid for row in rows):
    commit()
All match a predicate
One all call replaces a for-loop with a flag.
is_sorted = all(a <= b for a, b in zip(xs, xs[1:]))
Guard: all keys present
Confirm a dict has every required key before proceeding.
if all(k in data for k in required):
    process(data)

Examples

1. All truthy
all([1, 2, 3])
Returns
True
2. One falsy loses
all([1, 0, 3])
Returns
False
3. Empty is True
all([])
Returns
True
4. Generator + short-circuit
all(x > 0 for x in nums)
Returns
True or False; stops at first non-positive

Pitfalls

1. all([]) is True, not False
The empty case flips the intuitive answer. Python follows math: "every element of the empty set satisfies X" is vacuously true. Mirrors any([]) which is False for the same reason.
Wrong expectation
if all([]):
    print("truthy")
else:
    print("falsy")
truthy
Guard for empty
if items and all(items):
    ...
explicit intent
2. Falsy is not the same as False
all treats 0, "", None and [] as falsy — not just literal False. Silent failures if you meant "all values are True" strictly.
Surprising False
all([True, 1, ""])
False # empty string is falsy
Explicit equality
all(x is True for x in items)
only literal True passes
3. all on a generator consumes it
After a falsy hit, the generator is partially consumed — subsequent iteration skips what was already checked.
Half-consumed
g = (x for x in [1, 0, 5])
all(g)     # False (stops at 0)
list(g)    # [5] — 1 and 0 are gone
[5]
Materialize first
items = [x for x in source]
all(items)
list(items)
full list preserved

When to use

Use it
  • "Do all items satisfy X?"
  • Validation over rows, records, batch items
  • Short-circuit checks on large or generated iterables
Reach for something else
  • "Does at least one satisfy X?" → any
  • Counting matches → sum(cond for x in xs)
  • Strict "every value equals True" → `all(x is True for x in xs)`

Notes

Complexity
O(k) where k is the position of the first falsy item; O(n) worst case
Return
bool — always True or False, never the item itself
CPython impl
Python/bltinmodule.c :: builtin_all — thin loop over the iterator with early exit
Memory
O(1) — no buffering
Thread-safe
The scan is safe; the source should not mutate concurrently

FAQ

It matches the universal quantifier: "for every x in [], x is truthy" — trivially true, because there is no x that could disprove it. any([]) is False by the mirror convention.

History

2.5
any() and all() introduced together.