In Python, any(iterable) returns True as soon as it finds one truthy item, and False if none is truthy. An empty iterable gives False, because nothing was found.
Pair it with a generator expression to ask “does anything match?”: any(word.startswith("x") for word in words). It short-circuits on the first match. not any(...) means “none match”, and all() is its counterpart for “every item matches”.
any() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass an iterable, usually a generator expression.
python
found = any(iterable)
found = any(condition(x) for x in items)
Call
What it does
any([0, 0, 1])
Returns True.
any([0, "", None])
Returns False.
any([])
Returns False.
any(x < 0 for x in xs)
Is there a negative number?
For a simple membership test of one value, value in items is clearer than any(x == value for x in items).
Library declarations
python
any(iterable, /)
Parameters
Parameter
Description
iterable
Required. Any iterable; each item is tested for truthiness.
Return Value
Return Value
Description
bool
True if at least one item is truthy, otherwise False.
Python Examples
Example 1 — How any() Treats Falsy Items
Check lists of falsy values, a list with one truthy value, an empty list, and a string.
One is enough. 7 is truthy, so the second list returns True.
Empty is False. There is nothing truthy in an empty list.
Strings are iterables. Each character is a non-empty string, so "abc" gives True.
Example 2 — Does Anything Match?
Check a file extension against a list, and look for unpaid or specific orders.
python
filename = "report_final.pdf"
print(any(filename.endswith(ext) for ext in [".pdf", ".docx"]))
orders = [{"id": 1, "paid": True}, {"id": 2, "paid": False}]
print(any(not o["paid"] for o in orders))
print(any(o["id"] == 5 for o in orders))
Output
True
True
False
How It Works
Several options. The generator tries each extension until endswith() matches ".pdf".
Tuple shortcut.filename.endswith((".pdf", ".docx")) does the same in one call.
Any unpaid? Order 2 is not paid, so not o["paid"] is true for it.
No match. No order has id 5, so the last line is False.
Example 3 — any() Stops at the First Match
Print inside the test to see the early exit, and use not any() to find words without vowels.
python
def is_negative(n):
print("testing", n)
return n < 0
print(any(is_negative(n) for n in [4, -2, -9]))
print(any(is_negative(n) for n in []))
words = ["tree", "sky", "rhythm"]
print([w for w in words if not any(c in "aeiou" for c in w)])
Output
testing 4
testing -2
True
False
['sky', 'rhythm']
How It Works
Early exit. -2 is the first negative number, so -9 is never tested.
Empty, no calls. Without items, the function never runs and the answer is False.
None match.not any(...) is true when no character is a vowel.
Inside a comprehension. sky and rhythm have no a, e, i, o, or u; tree has an e.
Frequently Asked Questions
any() is True only if it finds a truthy item. An empty iterable has none, so the result is False.
any() needs at least one truthy item; all() needs every item to be truthy.
any(word in text for word in words).
No, only True or False. Use next((x for x in items if test(x)), None) to get the match itself.
It does the same work but runs in C and stops at the first match, so it is typically as fast or faster, and shorter.