How do Python's any and all functions work?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
any() and all() are built-in Python functions for aggregating truth values across an iterable. They are compact, short-circuiting, and ideal for validation, filtering gates, and guard conditions, but they only make sense if you are clear on Python truthiness and on the surprising behavior of empty iterables.
What any() Does
any(iterable) returns True if at least one element is truthy:
Output:
It stops as soon as it finds the first truthy value. That short-circuit behavior matters for both performance and side effects.
What all() Does
all(iterable) returns True only if every element is truthy:
Output:
It stops at the first falsy value.
Empty Iterable Behavior
This is the part that surprises people most:
Output:
any([]) is False because there is no truthy element.
all([]) is True because there is no violating element. This is called vacuous truth. It is mathematically consistent, but you should still think carefully about whether it matches your business rule.
If you need "non-empty and all pass," write that explicitly:
Use Generator Expressions for Real Predicates
Most useful code combines any() or all() with a condition:
This is usually better than writing a manual loop when the logic is a simple existence or universal check.
Prefer Generators Over List Comprehensions
A generator expression is usually the right shape:
This is usually better than:
The generator version avoids creating an unnecessary list and preserves short-circuit behavior more naturally.
Truthiness Rules Matter
any() and all() do not require explicit booleans. They use Python truthiness. Values commonly treated as falsy include:
- '
False' - '
0' - '
0.0' - '
""' - '
[]' - '
{}' - '
None'
That means this can be misleading if zero is valid data:
Output:
If the real condition is "not None," write that explicitly:
Short-Circuiting Can Affect Side Effects
Because both functions stop early, side-effect-heavy expressions can behave in surprising ways. For example:
Once 0 fails the predicate, the remaining element is never checked.
That is usually a feature, but if you need to inspect every failure, you should collect the failures explicitly rather than relying on all().
A Readability Rule
any() and all() make code more declarative when the predicate is simple:
- "is there any failing item"
- "do all records satisfy this rule"
If the predicate becomes long and tangled, extract it into a named helper function. That keeps the expression readable while preserving the concise API.
Common Pitfalls
- Forgetting that
all([])returnsTrueandany([])returnsFalse. - Treating
0or""as invalid accidentally because they are falsy. - Using list comprehensions when generator expressions are enough.
- Relying on side effects inside the predicate and forgetting that short-circuiting stops early.
- Writing one very dense nested
any()orall()expression that nobody can read later.
Summary
- '
any()returnsTrueif at least one element is truthy.' - '
all()returnsTrueonly if every element is truthy.' - Both short-circuit, which helps efficiency.
- Empty iterable behavior is different for each function and often surprises people.
- Use generator expressions and explicit predicates when truthiness alone is not the real rule.
Related reading
- How do threads work in Python, and what are common Python-threading specific pitfalls?
- How do we determine the number of days for a given month in python
- How do you access the query string in Flask routes?
- How do you add a Dictionary of items into another Dictionary
- How do you alter the size of a Pytorch Dataset?
- How do you convert a time.struct_time object into a datetime object?
- How do you create a boolean mask for a tensor in Keras?
- How do you create a daemon in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.