Learn by reading through in order

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.

How all() decides
[T, T, T]all()True[T, F, T]all()False[ ] (empty)all()True

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

Check whether every item in a shopping cart is in stock.

① Define in_stock = [True, True, True, False].

② Print the result of all(in_stock).

③ With if all(in_stock):, print "Proceed to checkout" when everything is in stock, otherwise "Some items are out of stock".

(The explanation appears once you run the code correctly.)

Python Editor

Run code to see output

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.

FunctionReturns True whenReturns False whenOn an empty list
all()Every element is TrueAt least one element is FalseTrue
any()At least one element is TrueEvery element is FalseFalse
# 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

Check whether at least one survey respondent says they want to attend.

① Define want_to_join = [False, False, False, True, False].

② Print the result of any(want_to_join).

③ With if any(want_to_join):, print "Hold the event" when there's at least one yes, otherwise "Cancel".

Python Editor

Run code to see output

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.

Behavior on an empty collection
[ ] (empty)all()True[ ] (empty)any()False
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(...):.

Confirm the empty-list behavior firsthand.

① Define empty = [].

② Print the results of all(empty) and any(empty).

③ Use if empty and all(empty): to print "All items OK" only when the list is non-empty and everything is True (verify nothing is printed for the empty list).

Python Editor

Run code to see output

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()".

Use a list of test scores to make pass/fail-style judgments.

① Define scores = [78, 82, 65, 91, 55].

② Decide whether everyone scored 60 or higher (using all) and whether at least one person scored 90 or higher (using any), and assign them to all_pass and has_genius.

③ Print both results.

Python Editor

Run code to see output

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".

QUIZ

Knowledge Check

Answer each question one by one.

Q1What does this code print?
print(all([1, 2, 0, 3]))

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?