Python any() Method
In this tutorial we will learn about python any() method and its uses.
Python’s any() function tests whether at least one element in an iterable is truthy. It accepts a single argument – any iterable such as a list, tuple, set, dictionary, string, or generator – and returns True if any element evaluates to True under Python’s truth-testing rules. If the iterable is empty or all elements are falsy, any() returns False. Like its counterpart Python all(), any() short-circuits evaluation: it stops iterating as soon as it finds the first truthy value, making it efficient for large datasets. This function is commonly used for existence checks (does any item match a condition?), permission validation (does the user have at least one required role?), and search operations. It pairs naturally with generator expressions, allowing concise and readable one-liners like any(x > 100 for x in prices). For converting individual values to Boolean, see Python bool().
Introduction to Python any()
When writing Python programs, you frequently need to determine whether at least one item in a collection satisfies some condition. You could write a loop, set a flag variable, and break early — but Python’s built-in any() function handles all of that in a single, expressive call. It is part of Python’s set of functional programming utilities alongside all(), map(), filter(), and zip(), and it is available without any imports in every version of Python 3.
The power of any() lies in its simplicity and its short-circuit behaviour. The moment any() encounters a truthy element, it immediately returns True without checking the remaining items. This means that on a list of one million numbers, if the very first number is non-zero, any() returns in constant time rather than scanning the whole list. This property makes it a go-to choice in performance-sensitive code paths such as data validation pipelines and real-time monitoring systems.
Understanding what Python considers “truthy” and “falsy” is essential to using any() correctly. Falsy values include 0, 0.0, 0j, "" (empty string), [] (empty list), () (empty tuple), {} (empty dict or set), None, and False. Every other value is truthy. This means any([0, None, "", [], False]) returns False, while any([0, None, 1]) returns True because 1 is truthy.
Syntax of any() Method
1
any(iterable)
Parameter
| Parameter | Type | Required | Description |
|---|---|---|---|
iterable | iterable | Yes | Any Python iterable: list, tuple, set, dict, string, generator expression, etc. |
The function takes exactly one argument. Passing zero arguments or more than one argument raises a TypeError. The iterable can be of any length, including empty, and can contain elements of mixed types.
Return Value
any() always returns a plain Python bool — either True or False. It never raises an exception due to the content of the iterable (though it will propagate exceptions raised by the iterable’s elements during iteration).
What does any() return?
The any() function returns True if at least one element in the iterable is truthy. It returns False if the iterable is empty or all elements are falsy.
When should you use any()?
Use any() when you need to check whether at least one item in a collection meets a condition. This is ideal for searching, existence checks, and early exit logic where finding a single match is sufficient.
What is python any() method?
The any() is a built-in method available in python, which will return True if any element is present in an iterable like list, tuple, set, dictionary, or loop in python.
If the iterable is empty or any elements are not present, the any() method will return False.
Syntax of any() method.
1
any(iterable)
Python any() Parameter
The any() method will take an iterable, including list, tuple, set, dictionary, string, and loop in python.
Let’s see a basic example of any() method.
Example 1: Using any() on Python Lists
1
2
3
4
5
6
7
8
my_list = [1, 2, 3, 4, 5]
print(any(my_list)) # True — at least one non-zero value
my_list_2 = []
print(any(my_list_2)) # False — empty iterable
my_list_3 = [0, False]
print(any(my_list_3)) # False — all values are falsy
Output:
1
2
3
True
False
False
As we can see, the first list is not empty. That’s why any() method is returning True because it contains elements. The second list is empty, hence returning False. Note that any() considers 0 and False as falsy values, hence returning False for the third list.
The any() method works similarly for tuples and sets like lists.
Example 2: Using any() on Python Strings
When applied to a string, any() iterates over its characters. An empty string has no characters, so it returns False. A string containing at least one character (including whitespace) will have at least one truthy element.
1
2
3
4
5
6
7
8
my_string = "This is Python"
print(any(my_string)) # True — has non-empty characters
my_string_2 = ""
print(any(my_string_2)) # False — empty string has no characters
my_string_3 = " "
print(any(my_string_3)) # True — a space character is truthy
Output:
1
2
3
True
False
True
This behaviour is useful when you need to quickly verify that a string variable is not empty and contains actual content.
Example 3: Using any() on Python Dictionaries
When any() is applied to a dictionary, it iterates over the keys, not the values. This is a common gotcha: even if all values are falsy, any() returns True as long as at least one key is truthy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
my_dict = {1: True}
print(any(my_dict)) # True — key 1 is truthy
my_dict = {0: False}
print(any(my_dict)) # False — key 0 is falsy
my_dict = {0: 'False', 1: 'True'}
print(any(my_dict)) # True — key 1 is truthy
my_dict = {0: 'False'}
print(any(my_dict)) # False — only key is 0 (falsy)
my_dict = {}
print(any(my_dict)) # False — empty dict
my_dict = {0: 'False', False: 0}
print(any(my_dict)) # False — 0 and False are the same key
Output:
1
2
3
4
5
6
True
False
True
False
False
False
In the case of dictionaries, if all keys (not values) are false or the dictionary is empty, any() returns False. If at least one key is truthy, any() returns True.
Example 4: Using any() with Generator Expressions
The most powerful way to use any() is combined with a generator expression. This lets you check a condition across all elements without creating an intermediate list in memory.
1
2
3
4
5
6
7
8
9
10
11
numbers = [4, 8, 15, 16, 23, 42]
# Check if any number is greater than 20
print(any(n > 20 for n in numbers)) # True (23 and 42 qualify)
# Check if any number is negative
print(any(n < 0 for n in numbers)) # False
# Check if any string starts with 'P'
words = ["apple", "Python", "cherry"]
print(any(w.startswith("P") for w in words)) # True
Output:
1
2
3
True
False
True
Because generator expressions are lazy, any() stops consuming the generator as soon as it finds a match. This is far more efficient than checking len([n for n in numbers if n > 20]) > 0, which always builds the entire filtered list.
Rules of any() method
- The
any()method will return a boolean as a value. - If there is at least one element in an iterable that is true,
any()will returnTrue. - If all elements in an iterable are
Falseor the iterable is empty,any()will returnFalse. - If all the values are true in the iterable,
any()will returnTrue. - If all the values are false in the iterable,
any()will returnFalse. - If one value is true and others are false,
any()will returnTrue. - If one value is false and others are true,
any()will returnTrue. - If the iterable is empty,
any()will returnFalse.
Real-World Use Cases
1. Permission and Role Checking
In web applications, users often have multiple roles and you need to verify that they hold at least one of the roles required to access a particular resource.
1
2
3
4
5
6
7
8
user_roles = ["viewer", "editor"]
required_roles = ["admin", "editor", "moderator"]
if any(role in required_roles for role in user_roles):
print("Access granted")
else:
print("Access denied")
# Output: Access granted
This replaces a verbose nested loop with a clean, readable one-liner. The short-circuit behaviour also means the check stops as soon as the first matching role is found.
2. Data Validation Pipelines
When validating records, you may want to flag a record as invalid if any of its fields fail a check.
1
2
3
4
5
6
7
8
9
10
11
12
13
def is_invalid_record(record):
checks = [
record.get("age", 0) < 0,
record.get("name", "") == "",
record.get("email", "").count("@") != 1,
]
return any(checks)
record = {"age": 25, "name": "Alice", "email": "[email protected]"}
print(is_invalid_record(record)) # False — all checks pass
bad_record = {"age": -5, "name": "Bob", "email": "[email protected]"}
print(is_invalid_record(bad_record)) # True — age is negative
3. Monitoring and Alerting
In a system monitoring context, you might receive a batch of metric readings and want to know whether any reading exceeds a threshold.
1
2
3
4
5
6
7
response_times_ms = [120, 230, 180, 5400, 95]
if any(t > 5000 for t in response_times_ms):
print("ALERT: At least one request exceeded 5 seconds!")
else:
print("All response times within acceptable range.")
# Output: ALERT: At least one request exceeded 5 seconds!
Edge Cases and Gotchas
Empty iterables always return False. any([]), any(()), any({}), and any("") all return False. This can be surprising when you expect at least a default True. Always consider whether an empty iterable is a valid input in your logic.
Dictionaries iterate over keys, not values. any({"a": False}) returns True because the key "a" is truthy. If you want to check values, use any(d.values()).
Short-circuit evaluation means side effects may not run. If your iterable elements have side effects (e.g., functions that log something), some of those calls may be skipped if an earlier element is truthy.
0 and False are equal as dictionary keys. {0: "zero", False: "false"} is actually a dict with only one key because 0 == False in Python.
Nested iterables are not flattened. any([[1, 2], []]) returns True because the first element [1, 2] is a non-empty list, which is truthy — regardless of its contents.
Comparison with Related Functions
| Function | Behaviour | Returns True when… |
|---|---|---|
any(it) | Short-circuits on first truthy element | At least one element is truthy |
all(it) | Short-circuits on first falsy element | Every element is truthy (or empty) |
bool(x) | Converts a single value to Boolean | x is truthy |
filter() | Returns an iterator of truthy elements | N/A — not a boolean check |
A useful identity: any(iterable) is logically equivalent to not all(not x for x in iterable), though the direct form is always preferred for readability.
FAQ
Q1: Does any() work with generator expressions, and is it more efficient than using a list comprehension?
Yes. Using any() with a generator expression — any(condition for x in iterable) — is significantly more memory-efficient than building an intermediate list with a list comprehension and then checking its length. It also benefits from short-circuit evaluation: as soon as a truthy value is found, iteration stops. A list comprehension always builds the entire list before any check can happen.
Q2: What happens if the iterable passed to any() raises an exception during iteration?
The exception propagates normally. any() does not catch exceptions thrown by the iterable or its elements. For example, any(1/x for x in [1, 0, 2]) will raise a ZeroDivisionError when it reaches 0. If you need to handle such cases, wrap the generator expression in a helper function with error handling.
Q3: Can I use any() to check values in a dictionary instead of keys?
Yes. By default any(my_dict) checks the keys. To check values, pass my_dict.values() explicitly: any(my_dict.values()). To check both keys and values simultaneously, you can iterate over my_dict.items(), which yields (key, value) tuples — a non-empty tuple is always truthy, so this effectively checks whether the dict is non-empty.
Related Functions
- Python all() – return
Trueif every element is truthy (the logical counterpart ofany()). - Python bool() – convert a single value to Boolean.
- Python callable() – check if an object is callable.
