Python Python any(): Return True If At Least One Element Is Truthy — Syntax, …
page info

본문

Python any(): Check if at least one element is truthy
The built-in any()
function returns True
if at least one element in an iterable is truthy; otherwise it returns False
. It evaluates lazily and short-circuits on the first truthy value, which makes it efficient for large datasets and streaming sources.
Syntax
any(iterable)
Parameter: Any iterable (list, tuple, set, dict, generator, iterator, etc.).
Returns: True
if one or more elements are truthy; False
if none are truthy or the iterable is empty. Note that any([])
returns False
by definition.
Truthiness rules
- Falsy:
False
,None
, numeric zero (0
,0.0
), empty containers ([]
,()
,{}
,set()
,""
), and objects whose__bool__()
returnsFalse
or__len__()
returns0
. - Truthy: Everything else, including non-zero numbers, non-empty strings, and non-empty containers.
Quick examples
print(any([0, "", None])) # False (all falsy)
print(any([0, "", 5])) # True (5 is truthy)
print(any([])) # False (empty iterable)
print(any(x > 10 for x in [3, 12, 7])) # True (12 > 10)
Short-circuiting and generators
any()
consumes iterables lazily. With generators or comprehensions, evaluation stops at the first truthy element, saving time and memory.
def has_missing(values):
# True if any value is None or empty string
return any(v is None or v == "" for v in values)
row = ["id42", "Alice", "", "NY"]
print(has_missing(row)) # True (stops when "" is found)
Common use cases
- Validation: Detect presence of errors or flags in a record.
- Threshold checks: Determine if any measurements exceed a limit.
- Feature existence: Confirm at least one optional field is filled.
temperatures = [18, 21, 33, 25]
print(any(t > 30 for t in temperatures)) # True
Pitfalls and caveats
- Dicts iterate keys:
any({0: False, 1: False})
checks keys (0
,1
), not values. Useany(d.values())
to test values. - Empty iterable is False: If you need to ensure non-empty and at least one truthy element, combine checks appropriately.
- Whitespace strings: Non-empty strings like
" "
are truthy; trim or validate content explicitly.
d = {"a": False, "b": True}
print(any(d)) # True (keys "a", "b" are truthy)
print(any(d.values())) # True (checks actual boolean values)
Performance notes
- Laziness: Stops early on the first truthy element, avoiding full traversal.
- Memory efficiency: Prefer generators (e.g.,
(pred(x) for x in data)
) over lists to avoid temporary allocations. - Expensive predicates: Cache or pre-filter to maximize benefits of short-circuiting.
Comparison with all()
- any(): True if at least one element is truthy; empty iterable yields False.
- all(): True only if every element is truthy; empty iterable yields True.
vals = [0, "", None, 5]
print(any(vals)) # True (5 is truthy)
print(all(vals)) # False (0, "", None are falsy)
FAQ
- Does
any()
work with custom objects? - Yes. Truthiness is determined by
__bool__()
or, if absent, by__len__()
being non-zero. - How do I require non-empty and at least one truthy element?
- Combine checks:
iterable and any(iterable)
orlen(iterable) > 0 and any(predicate(x) for x in iterable)
. - Can I detect which element caused
any()
to return True? any()
does not return the element. Iterate manually or usenext()
with a generator to retrieve it.
Related keywords
Python any, truthiness, short-circuit evaluation, generators, validation, iterable, any vs all, boolean context, performance
comment list
There are no registered comments.