Last modified: Nov 19, 2026
Python any() and all() Functions
Python offers two built-in functions for checking truth values in collections. They are any() and all(). These functions are simple but powerful. They help you write cleaner and shorter code.
In this article, we will explain how both functions work. We will also show examples and common use cases. By the end, you will know exactly when to use each one.
What Is the any() Function?
The any() function returns True if at least one element in an iterable is truthy. If all elements are falsy, it returns False. An iterable can be a list, tuple, set, dictionary, or any object that can be looped over.
Here is the basic syntax:
any(iterable)
The function stops as soon as it finds a truthy value. This makes it efficient for large collections. If the iterable is empty, any() returns False.
any() Example with a List
Let's look at a simple example. We have a list with some falsy and one truthy value.
# Check if any value is truthy
values = [0, 0, 5, 0]
result = any(values)
print(result) # Output: True
True
The list contains the number 5, which is truthy. So any() returns True.
Now consider a list where all values are falsy.
# All values are falsy
values = [0, 0, 0, 0]
result = any(values)
print(result) # Output: False
False
Since no element is truthy, the result is False.
any() with Strings and Empty Iterables
Strings are also iterables. An empty string is falsy. A non-empty string is truthy.
# Check any characters in a string
print(any("")) # Output: False
print(any("hello")) # Output: True
False
True
For an empty iterable, any() always returns False.
# Empty list
print(any([])) # Output: False
False
What Is the all() Function?
The all() function returns True only if every element in the iterable is truthy. If even one element is falsy, it returns False. For an empty iterable, all() returns True.
Here is the syntax:
all(iterable)
Like any(), all() stops early. It stops as soon as it finds a falsy value. This makes it fast for large data sets.
all() Example with a List
Let's test a list where all values are truthy.
# All values are truthy
values = [1, 2, 3, 4]
result = all(values)
print(result) # Output: True
True
Every element is non-zero, so the result is True.
Now add a zero to the list.
# One falsy value makes all() return False
values = [1, 2, 0, 4]
result = all(values)
print(result) # Output: False
False
The zero is falsy, so all() returns False. The function stops at the zero and does not check the rest.
all() with Empty Iterables
An empty iterable returns True for all(). This is because there are no falsy values to fail the check.
# Empty list returns True
print(all([])) # Output: True
True
Truthiness in Python
To use any() and all() well, you must understand truthiness. In Python, many values are considered falsy. These include:
- None
- False
- Zero of any numeric type: 0, 0.0, 0j
- Empty sequences: "", [], ()
- Empty mappings: {}
All other values are truthy. This includes non-empty strings, non-zero numbers, and non-empty collections.
You can check object identity to understand how Python treats values. For more details, see this guide on the Python id Function and Object Identity.
Practical Use Cases
These functions are useful in many real-world scenarios. Let's explore a few.
Checking User Input
You can use any() to check if any required field is filled.
# Check if any field has content
fields = ["", "Alice", ""]
if any(fields):
print("At least one field is filled.")
At least one field is filled.
Validating All Conditions
Use all() to make sure every condition is met.
# Check if all conditions are True
conditions = [True, True, False]
if not all(conditions):
print("Not all conditions are met.")
Not all conditions are met.
Combining with map() and filter()
You can combine these functions with others like map() and filter(). For example, you can check if any number in a list is even.
# Check if any number is even
numbers = [1, 3, 5, 8]
has_even = any(map(lambda x: x % 2 == 0, numbers))
print(has_even) # Output: True
True
For more on mapping, read about the Python map Function Explained. You can also learn about the Python filter Function with Examples.
Working with Dictionaries
When you iterate over a dictionary, you get its keys. You can use any() or all() on the keys.
# Check if any key is truthy
data = {0: "zero", 1: "one"}
print(any(data)) # Output: True
True
To learn more about iterating over dictionaries, see how to enumerate a dictionary in Python.
Short-Circuit Evaluation
Both functions use short-circuit evaluation. This means they stop as soon as the result is clear.
For any(), it stops at the first truthy value. For all(), it stops at the first falsy value. This behavior saves time and resources.
Here is an example that shows the early stop.
# all() stops at the first falsy value
def check(x):
print(f"Checking {x}")
return x > 0
values = [1, 2, -1, 4]
print(all(check(x) for x in values))
Checking 1
Checking 2
Checking -1
False
The function never checks the 4 because -1 already made the result False.
Common Mistakes
Beginners often confuse the two functions. Remember: any() needs just one truthy value. all() needs every value to be truthy.
Another mistake is using them on non-iterable objects. Always pass a list, tuple, set, or other iterable.
Also, be careful with generators. They are iterators, and they can be consumed. If you use a generator with any() or all(), it will be exhausted after the call. For more on generators, read Python Generators and yield Explained.
Conclusion
Python's any() and all() functions are simple yet powerful tools. They help you write concise, readable code for checking truth values in collections.
Use any() when you need at least one truthy value. Use all() when every value must be truthy. Remember the rules for empty iterables and short-circuit evaluation.
With these examples and tips, you can now use both functions confidently in your Python projects.