Python all() Function: Syntax and 5 Examples

If you want to check that every item in a list or other iterable passes a test in Python, the Python all() function is what you need. It takes one iterable and gives you back True only when every item is truthy.

Type this into a new Python file in your editor and run it:

print(all([3, 7, 12]), all([3, 0, 12]))
# True False

In this tutorial, I’ll show you conditions, dictionaries, ZIP codes and early stopping, all run on Python 3.12.5.

Python all() Syntax

all() takes exactly one argument. Type it in your Python file:

all(iterable, /)
ParameterRequiredDefaultDescription
iterableYes–A list, tuple, set, dict, string or generator (positional-only)

Return value: True when every item is truthy or the iterable is empty, otherwise False. Truthy follows the rules of the Python bool() function.

When to Use all() in Python

  • Checking that every amount is positive
  • Validating required form fields
  • Testing each character of a string
  • Replacing a loop with a flag variable

An empty list passes, so check if a list is empty first when that matters.

Example 1: Check if All Values in a List Are True

Let me start with a list of daily sales. To run it:

  1. Paste the code into any editor.
  2. Save it as all_basic.py in a folder.
  3. In Command Prompt, go to that folder, type python all_basic.py and press Enter.
daily_sales = [1250, 980, 1675, 720]
print(all(daily_sales))

daily_sales.append(0)
print(all(daily_sales))
print(all([]))

Output:

True
False
True
Python all() function returning True for a list of sales numbers and False after a zero is added
One zero turns the result False.

How does this work? Every number was non-zero, so you get True. Adding 0 made it False, and the empty list printed True.

Example 2: Check a Condition for Every Item with all()

Usually you pass a condition. Save this as all_orders.py and run it in Command Prompt:

orders = {"#5101": 129.99, "#5102": 54.50, "#5103": -12.00, "#5104": 89.00}

print(all(amount > 0 for amount in orders.values()))
print(all(amount < 500 for amount in orders.values()))

Output:

False
True
Python all() with a generator expression checking that every order amount is positive
Order #5103 is negative.

How does this work? The -12.00 refund failed amount > 0, so you get False. Every amount was under 500.

Pro Tip: Skip the square brackets of a list comprehension so all() can stop early.

Example 3: Check Required Keys in a Python Dictionary

Here I validate a signup. Save this as all_required.py and run it in Command Prompt:

signup = {"name": "Liam Carter", "email": "", "state": "OH"}

print(all(key in signup for key in ("name", "email", "state")))
print(all(signup.values()))

Output:

True
False
Python all() checking required keys and empty values in a signup dictionary
Liam has the key but an empty email.

How does this work? All three keys exist, but the empty email made all(signup.values()) False.

Example 4: Validate a US ZIP Code with all()

all() works on each character of a string. Save this as all_zipcodes.py and run it in Command Prompt:

zip_codes = ["10001", "73301", "9021O", "60614"]

for code in zip_codes:
    valid = len(code) == 5 and all(ch.isdigit() for ch in code)
    print(code, valid)

Output:

10001 True
73301 True
9021O False
60614 True
Python all() with isdigit() validating five-digit US ZIP codes
The letter O fails the check.

How does this work? “9021O” ends with the letter O, which fails the isdigit() method. For case, check if a string is all uppercase.

Example 5: See How all() Stops at the First False

A print shows which items all() reads. Save this as all_early.py and run it in Command Prompt:

def is_paid(invoice):
    print("checking", invoice[0])
    return invoice[1]

invoices = [("INV-1101", True), ("INV-1102", False), ("INV-1103", True)]
print(all(is_paid(inv) for inv in invoices))

Output:

checking INV-1101
checking INV-1102
False

How does this work? all() stopped at INV-1102 and never checked INV-1103. It short-circuits like and in logical operators in Python.

all() vs any() in Python

FunctionReturns True whenEmpty iterable
all()Every item is truthyTrue
any()At least one item is truthyFalse

For “at least one” rules, use the Python any() function.

Common Mistakes with Python all()

Save this as all_mistakes.py and run it in Command Prompt:

print(all({"open": 0}), all("000"), all([]))

for args in [(1, 2, 3), (5,)]:
    try:
        all(*args)
    except TypeError as err:
        print("TypeError:", err)

Output:

True True True
TypeError: all() takes exactly one argument (3 given)
TypeError: 'int' object is not iterable
Common Python all() mistakes: dictionary keys, a string of zeros, an empty list and TypeErrors
Three silent surprises, two TypeErrors.
  1. On a dict, all() checks keys. Use .values().
  2. “000” is True: its characters aren’t empty.
  3. An empty list returns True.
  4. Pass one iterable, not separate values or a number.

Wrapping Up the Python all() Function

The Python all() function returns True when every item passes and False at the first one that fails. Pair it with a condition for readable rules.

Practice it inside a Python for loop, then browse more Python built-in functions and methods.

What People Ask About Python all() Function

Why does all() return True for an empty list?

all([]) returned True because no item fails.

What type does all() return?

Always bool, never the item itself.

Does all() treat None as False?

Yes. all([None]) returned False.

How do I check that all elements in a list are equal?

Compare each to the first: all(p == prices[0] for p in prices) returned True for three $19.99 prices.

Does all() work on a set?

Yes, any iterable works. all({0, 5}) returned False because 0 is falsy. See all() in the Python docs.

More Python posts on checking values: