Q1What does this code print?print(all([1, 2, 0, 3]))
Bulk Checks with all() and any()
Use Python's all() and any() to check whole collections at once — every example runs live in your browser.
all() — True only when every element is True
all(iterable) returns True when every element of the list, tuple, or set you pass in is True (or evaluates as truthy), and False if even one element is False.
True if every element is True. False if any element is False. True when given an empty iterable.
The same truthiness rules from the previous article apply: 0, empty strings, and empty lists are treated as False, so a list of numbers containing 0 makes all() return False.
# A list of bools: False if any element is False
print(all([True, True, True])) # True
print(all([True, False, True])) # False
# A list of numbers: False if 0 is in there
print(all([1, 2, 3])) # True
print(all([1, 0, 3])) # False
# Are all required form fields filled in?
form_values = ["Alice", "alice@example.com", "100 Main St, Springfield"]
if all(form_values):
print("Ready to submit")
else:
print("Some fields are empty")
# Output: Ready to submit
any() — True if at least one element is True
any(iterable) returns True if at least one element is True, and False only when every element is False. Where all() is the strict check that demands a unanimous pass, any() is the lenient check that's happy with a single match.
| Function | Returns True when | Returns False when | On an empty list |
|---|---|---|---|
| all() | Every element is True | At least one element is False | True |
| any() | At least one element is True | Every element is False | False |
# A list of bools: True if at least one is True
print(any([False, False, True])) # True
print(any([False, False, False])) # False
# Retry if at least one error flag was raised
error_flags = [False, False, True, False]
if any(error_flags):
print("A retry is needed")
else:
print("All succeeded")
# Output: A retry is needed
Behavior when given an empty collection
When you pass in an empty list, all([]) returns True and any([]) returns False. This is counterintuitive, so be careful when the data could legitimately have zero elements.
print(all([])) # True <- there was "nothing False" to find, so True
print(any([])) # False <- there was "nothing True" to find, so False
# Example: a cart with no items registered yet
cart = []
if all(cart):
print("All items checked") # runs even when the cart has 0 items
# This may not match your intent, so check for empty first
Decide how to handle empty lists up front
When you use all() in the spirit of "if every member meets the condition…", 0 members still returns True. To prevent unintended early passes, check that the list isn't empty first, like if members and all(...):.
Combine with conditions to check in bulk
all() / any() truly shine when combined with comparison expressions. To check "are they all 18 or older?", build a list of >= 18 results from the ages and pass it in — the whole check fits on one line.
ages = [19, 20, 18, 25, 57]
# Build a list of bools first, then pass it to all (the easier 2-step version)
checks = [age >= 18 for age in ages]
print(checks) # [True, True, True, True, True]
print(all(checks)) # True
# Or write it inline (list comprehensions / generator expressions — covered next)
print(all(age >= 18 for age in ages)) # True
print(any(age >= 65 for age in ages)) # False
We'll cover list comprehensions like [age >= 18 for age in ages] in detail in the for chapter. For now, it's enough to get the feel of "you can evaluate a condition across a list and pass the result straight into all() / any()".
In this article, we learned all() and any() for checking the contents of lists or tuples in bulk.
all() requires everyone to pass, any() is happy with at least one, and on an empty input all is True while any is False. In the next article, we'll dig into the for loop itself — the engine behind the [... for ... in ...] expression that quietly showed up here as a "list of results".
Knowledge Check
Answer each question one by one.
Q2What does this code print?print(any([False, False, False]))
Q3When passed an empty list [], which combination of return values from all([]) and any([]) is correct?