Imagine you have a list of conditions—say, a list of booleans representing whether a user has completed certain steps in a registration process. How would you check if they’ve completed any of the steps? Or if they’ve completed all of them?
You could write a for
loop to manually check a flag variable, but this is a common enough task that Python, following its “batteries-included” philosophy, provides two concise, readable, and efficient built-in functions for exactly this purpose: any()
and all()
.
any()
: Is at Least One Item True?
The any(iterable)
function takes an iterable (like a list or tuple) and returns True
if at least one of the items in it is “Truthy”. If all the items are “Falsy” (or the iterable is empty), it returns False
.
As a quick refresher from Post #24, “Truthy” values are things like True
, non-empty strings, and non-zero numbers. “Falsy” values are things like False
, 0
, ""
, and None
.
The any()
function is also efficient because it “short-circuits”: it stops iterating and returns True
as soon as it finds the first Truthy item, without needing to check the rest of the collection.
# A list with at least one True value
results1 = [False, False, True, False]
print(f"any(results1): {any(results1)}")
# A list with only Falsy values
results2 = [0, "", False, None]
print(f"any(results2): {any(results2)}")
# An empty list is Falsy
results3 = []
print(f"any(results3): {any(results3)}")
The output will be:
any(results1): True
any(results2): False
any(results3): False
all()
: Are All Items True?
The all(iterable)
function also takes an iterable, but it’s stricter. It returns True
only if every single item in the iterable is “Truthy”.
This function also short-circuits. It stops iterating and returns False
as soon as it finds the first Falsy item.
# A list where all values are Truthy
results1 = [True, 1, "hello"]
print(f"all(results1): {all(results1)}")
# A list with one Falsy value (0)
results2 = [True, 1, "hello", 0]
print(f"all(results2): {all(results2)}")
The output shows the difference:
all(results1): True
all(results2): False
The Empty List Edge Case
There is one tricky edge case to remember with all()
: all([])
returns True
. This is because there are no Falsy items in an empty list, so the condition “all items are Truthy” is considered vacuously true. This is mathematically consistent but can sometimes be surprising if you’re not expecting it.
A Common Pattern: any()
with a Generator Expression
A very common and powerful pattern is to use any()
or all()
on a generator expression (from Post #127) to perform a check without building a full list in memory.
For example, let’s check if there are any negative numbers in a list.
numbers = [10, 25, -5, 8, 30]
# This creates a generator of booleans (False, False, True, False, False)
# any() then checks that generator for any True values.
has_negative = any(num < 0 for num in numbers)
print(f"Does the list contain any negative numbers? {has_negative}")
This is both highly readable and memory-efficient. It reads like an English sentence and stops processing as soon as it finds the -5
.
What’s Next?
any()
and all()
are perfect examples of the Pythonic philosophy. They replace verbose for
loops with a single, clear, and efficient function call that perfectly expresses your intent. If you find yourself writing a loop just to check for True
or False
values, reach for any()
or all()
instead.
any()
and all()
are just two of the many powerful, optimized functions Python provides for working with iterables. Python also has a special built-in module that is like a treasure chest of high-performance tools for creating and working with iterators. In Post #136, we will take a quick look at the itertools
module.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com