Python Python all(): Return True If All Elements Are Truthy — Syntax, Truthin…
page info
name Goposu datetime⏰ 25-10-11 01:33 hit👁️ 13 comment💬 0text

Python all(): Check if all elements are truthy
The built-in all()
function returns True
if every element in an iterable is truthy; otherwise it returns False
. It evaluates elements lazily and short-circuits on the first falsy value, making it efficient for large or streaming data.
Syntax
all(iterable)
Parameter: Any iterable (list, tuple, set, dict, generator, etc.).
Returns: True
if all elements are truthy, False
otherwise. For an empty iterable, all([])
returns True
by definition.
Truthiness rules
- Falsy:
False
,None
, numeric zero (0
,0.0
), empty containers ([]
,()
,{}
,set()
,""
), and objects with__bool__
returningFalse
. - Truthy: Everything else, including non-zero numbers, non-empty strings, and non-empty containers.
Quick examples
print(all([1, 2, 3])) # True
print(all([1, 0, 3])) # False (0 is falsy)
print(all(["a", "b", ""])) # False (empty string is falsy)
print(all([])) # True (empty iterable)
print(all(x > 0 for x in [2, 4])) # True (generator lazily evaluated)
Short-circuiting and generators
all()
consumes the iterable lazily. With generators or comprehensions, evaluation stops at the first falsy element, which saves time and memory.
def valid_row(row):
return all(field is not None and field != "" for field in row)
# Stops as soon as a field is None or empty
row = ["id123", "Alice", None, "NY"]
print(valid_row(row)) # False
Common use cases
- Validation: Ensure all required fields are present.
- Constraints: Verify all numbers meet a condition (e.g., non-negative).
- Flags: Confirm all toggles are enabled before proceeding.
numbers = [3, 5, 7]
print(all(n > 0 for n in numbers)) # True
Pitfalls and caveats
- Dicts iterate keys:
all({1: True, 0: True})
evaluates keys (1
and0
), not values. Useall(d.values())
to check values. - Empty iterable is True: This is by design; combine with a non-emptiness check if needed.
- String truthiness: Non-empty strings are truthy; ensure you check content (e.g., whitespace) explicitly.
d = { "a": True, "b": False }
print(all(d)) # True (keys "a" and "b" are truthy)
print(all(d.values())) # False (checks the actual boolean values)
Performance notes
- Laziness: Avoids evaluating the entire iterable when a falsy element appears early.
- Memory efficiency: Pair with generators to avoid building intermediate lists.
- Complex predicates: Cache or simplify expensive checks to benefit from short-circuiting.
Comparison with any()
- all(): True if every element is truthy; empty iterable yields True.
- any(): True if at least one element is truthy; empty iterable yields False.
vals = [0, "", None, 5]
print(any(vals)) # True (5 is truthy)
print(all(vals)) # False (0, "", None are falsy)
FAQ
- Does
all()
work with custom objects? - Yes. If an object defines
__bool__()
(or__len__()
), its truthiness is used. - How do I ensure non-empty and all truthy?
- Combine checks:
iterable and all(iterable)
orlen(iterable) > 0 and all(...)
. - Can I stop early on failure with logging?
- Use a loop to log the failing element, or wrap the predicate to log before returning
False
.
Related keywords
Python all, truthiness, short-circuit evaluation, generators, validation, iterable, any vs all, boolean context, performance
👍good0 👎nogood 0
comment list 0
There are no registered comments.