any()
Short-circuit "is anything truthy?" over any iterable.
Common call
if any(x.is_ready for x in jobs):
Returns
True or False
Replaces
the multi-line for-loop-with-flag pattern
Watch out
empty iterable → False (not True, not error)
any(iterableiterable — Any 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
any(['0', '0', '3', '0'])
True
any walks the iterable and stops the moment it sees a truthy item — a generator input would not be consumed past that point. Empty iterables return False by convention: there is no truthy item, so "does any exist?" is False. Falsy values in Python: 0, 0.0, "", [], {}, None, False.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| iterable | iterable | yes | Any iterable — list, tuple, generator, set, dict (keys), file lines. Items are evaluated for truthiness the same way `if item:` does. |
Return value
bool — True as soon as one item is truthy. False if every item is falsy or the iterable is empty.
Common patterns
Check a condition across items
Generator expression + any — reads like the English question.
has_admin = any(u.role == "admin" for u in users)
Substring in any string
One any call replaces a for-loop with a flag.
contains_error = any("error" in line for line in log)
Guard against an empty result
any is False on an empty iterable — often the correct default.
if not any(results): print("nothing found")
Examples
1. One truthy wins
any([0, 0, 3, 0])
Returns
True2. All falsy
any([0, "", None, False])
Returns
False3. Empty is False
any([])
Returns
False4. Generator + short-circuit
any(x > 100 for x in nums)
Returns
True or False; stops at first hitPitfalls
1. any([]) is False, not True
A common surprise — "nothing is anything" sounds like it could be True, but Python defines the empty case as False. Mirrors mathematical convention (existential over empty set).
Wrong expectation
if any([]): print("truthy") else: print("falsy")
falsy
Read the contract
any([]) # False all([]) # True
documented behaviour
2. Passing a value, not an iterable
any takes exactly one iterable — not several arguments.
Wrong shape
any(a, b, c)
TypeError: any() takes exactly one argument (3 given)
Wrap it
any([a, b, c])
True or False
3. any on a generator consumes it
After a truthy hit, the generator is partially consumed — subsequent iteration skips what was already checked.
Half-consumed
g = (x for x in [0, 3, 5]) any(g) # True (stops at 3) list(g) # [5] — 0 and 3 are gone
[5]
Materialize first
items = [x for x in source] any(items) list(items)
full list preserved
When to use
Use it
- "Does at least one item satisfy X?"
- Short-circuit checks over large iterables
- Generator expressions where you do not want to build a list
Reach for something else
- "Do all items satisfy X?" → all
- Counting matches → sum(cond for x in xs)
- Finding the item itself → next() with a generator
Notes
Complexity
O(k) where k is the position of the first truthy item; O(n) worst case
Return
bool — always True or False, never the item itself
CPython impl
Python/bltinmodule.c :: builtin_any — 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 existential quantifier: "does there exist an x in [] such that x is truthy?" — no, because there is no x at all. all([]) is True by the mirror convention.
History
2.5
any() and all() introduced together.