The Python any() function returns True if at least one element of an iterable is truthy, and False if none is, including when the iterable is empty. It behaves as an OR across the whole iterable, and we use it for questions such as “is any field filled in?” or “does any line contain an error?”.
mytuple = (0, 1, False)
bool_result = any(mytuple) # True, because 1 is truthy
Next, we go through the syntax, the behavior with tuples, lists, dictionaries and strings, short-circuiting with a generator, and the difference from the or operator. Results are from Python 3.14.
1. Syntax and Return Value
The function takes one required argument, any iterable such as a list, tuple, set, dictionary, string or generator.
any(iterable) -> bool
The return value is always a bool, and only an iterable without a single truthy element gives False.
| Elements of the iterable | any() returns |
|---|---|
| All truthy | True |
| Some truthy, some falsy | True |
| All falsy | False |
| No elements (empty) | False |
Falsy values are 0, 0.0, False, None and empty collections or strings. Everything else is truthy, including the strings “0”, ” “ and “False”.
2. any() with Tuple, List, Dictionary and String
The function treats every iterable the same way, but each type yields different elements. A dictionary yields its keys, and a string yields its characters.
t = any((0, 1, False)) # True
l = any([0, False]) # False
d1 = any({0: "False"}) # False, checks the key 0
d2 = any({0: "False", 1: "True"}) # True, the key 1 is truthy
values = any({"a": 0}.values()) # False, checks the values
s1 = any("") # False, no characters
s2 = any(" ") # True, a space is a non-empty string
To check dictionary values instead of keys, we pass d.values(). The string case shows that any() is not a way to test for blank text; s.strip() is.
3. any() with a Condition and Short-Circuiting
The common real use passes a generator expression with a condition. The function stops at the first truthy result, so the rest of the generator never runs.
log = ["INFO start", "ERROR disk full", "INFO stop"]
has_error = any("ERROR" in line for line in log) # True
numbers = iter([1, 3, 4, 5, 7])
found_even = any(n % 2 == 0 for n in numbers) # True, stops at 4
left = list(numbers) # [5, 7], never checked
A list comprehension such as any([cond for x in items]) builds the whole list first and loses that advantage, so we drop the square brackets.
4. any() vs the or Operator
The or operator returns one of its operands and needs each operand written out, while any() always returns a bool and takes a list whose length we do not know in advance.
first = 0 or "" or "red" # 'red'
flag = any([0, "", "red"]) # True
For the opposite check, whether every element is truthy, see Python all().
5. Checking a Password for Required Characters
Password rules are a natural fit for any(), because each rule asks whether at least one character of a kind exists. Each check stops at the first matching character.
password = "tea4Two"
has_digit = any(c.isdigit() for c in password) # True
has_upper = any(c.isupper() for c in password) # True
has_symbol = any(not c.isalnum() for c in password) # False
Combining the three answers with and, or with all() over a list of checks, gives the final verdict for a sign-up form.
6. any() with None, Membership and Speed
These edge cases decide whether any() is the right call or the wrong one.
6.1. What does any() return for a list of None values?
It returns False, because None is falsy. To ask whether any element is not None, including 0, we test for it with any(x is not None for x in items).
6.2. Is any() the same as the in operator?
No. The in operator checks whether one value is present, “tea” in items, while any() checks a condition, any(i.startswith(“t”) for i in items). For a single value, in is simpler.
6.3. Is any() faster than a for loop?
With a generator expression, it runs about as fast as a loop with break, because the condition is still Python code. The gain is shorter, clearer code that stops as early as the loop.
7. Conclusion
The any() function answers whether at least one element is truthy and returns False for an empty iterable. With a generator expression it reads like plain English and stops at the first match, and for dictionaries we pass values() when the values are what matter.
8. References
Happy Learning !!