01 / 21
✓ ?

Python's any() & all()

Master two powerful built-in functions for testing collections, and learn how short-circuit evaluation makes them fast.

Python

What Are any() and all()?

Python provides two built-in functions that answer simple yes/no questions about every item in an iterable (a list, tuple, set, generator, etc.).

any(iterable)
Returns True if at least one element is truthy. Think: "Is any of these true?"
all(iterable)
Returns True if every element is truthy. Think: "Are all of these true?"

A Visual Mental Model

any() True False False → True At least one ✓ all() True True True → True Every one ✓ any() False False → False None true ✕ all() True False → False Not all ✕ Returns True Returns False

Why Use any() & all()?

You could write a for loop with an if check every time. But any() and all() give you the same result in one readable line.

✓ Readable
Your intent is immediately clear to anyone reading the code.
✓ Concise
Replace 4-5 lines of loop logic with a single expression.
✓ Fast
Short-circuit evaluation means Python can stop early.

The Formal Definitions

# Equivalent loop for any() def any(iterable): for element in iterable: if element: return True return False
# Equivalent loop for all() def all(iterable): for element in iterable: if not element: return False return True

Short-Circuit Evaluation

Short-circuiting means Python stops scanning the iterable the moment the answer is determined: it doesn't need to look at the rest.

any([False, False, True, ?, ?, ?]) False Check → False Check → True STOP! → ? ? ? Skipped → Returns True (checked only 3 of 6) all([True, True, False, ?, ?, ?]) True Check → True Check → False STOP! → ? ? ? Skipped → Returns False (checked only 3 of 6)

any() vs all(): Key Distinction

any() all()
Question asked "Is at least one truthy?" "Are all truthy?"
Returns True when ≥ 1 truthy element Every element truthy
Returns False when All elements falsy ≥ 1 falsy element
Short-circuits on First truthy item First falsy item
Empty iterable False True
Logic analogy OR across elements AND across elements

The Empty Iterable Edge Case

>>> any([]) False # No items → nothing is truthy >>> all([]) True # No items → nothing is falsy (vacuous truth)
any([]) → False
"Is there at least one truthy element?" No elements exist, so the answer is no.
all([]) → True
"Are all elements truthy?" There are zero elements to violate the condition, so the answer is yes.

How to Apply Them

1
Identify the question. Are you checking if at least one item passes a test? Use any(). All items? Use all().
2
Write the condition as a generator expression. E.g., (x > 0 for x in nums) produces True/False per item.
3
Wrap it in any() or all(). The function handles the loop and short-circuiting for you.
4
Handle the empty case. Decide if an empty iterable should return True or False for your logic. Guard with an if if needed.

Example 1: Basic Usage

scores = [85, 92, 78, 95, 61] # Did anyone score above 90? any(s > 90 for s in scores) # True # Did everyone pass (score >= 60)? all(s >= 60 for s in scores) # True # Did everyone score above 80? all(s > 80 for s in scores) # False (61 and 78 fail)

Example 2: String Validation

password = "MyP@ss123" # Check all conditions for a strong password has_upper = any(c.isupper() for c in password) has_lower = any(c.islower() for c in password) has_digit = any(c.isdigit() for c in password) has_special = any(c in "!@#$%" for c in password) is_strong = all([has_upper, has_lower, has_digit, has_special]) # True: all four conditions met

Example 3: Proving Short-Circuiting

def check(x): print(f"Checking {x}") return x > 0 nums = [-1, -2, 3, 4, 5] any(check(n) for n in nums) # Output: # Checking -1 # Checking -2 # Checking 3 ← stops here! Returns True # Items 4 and 5 are never checked.

Generators vs List Comprehensions

# Generator expression (lazy, short-circuits) any(x > 100 for x in huge_list) # List comprehension (builds entire list first!) any([x > 100 for x in huge_list])
✓ Generator (no brackets)
Evaluates lazily. Combined with short-circuiting, Python may only check a few items.
✕ List (with brackets)
Builds the entire list in memory first, then passes it. Short-circuiting still works, but memory is already wasted.

Common Patterns in Practice

1
Validate all inputs: all(field != "" for field in form_data)
2
Check for any error: any(code >= 400 for code in status_codes)
3
Confirm all files exist: all(os.path.exists(f) for f in file_list)
4
Search for a keyword: any(keyword in line for line in document)
5
All tests passing: all(test.passed for test in test_results)

Common Mistakes

✕ Confusing any/all
Using all() when you need any(), or vice versa. Ask: "at least one?" vs "every single one?"
✕ Using a list comprehension
Writing any([...]) with brackets defeats lazy evaluation. Drop the brackets for a generator.
✕ Forgetting the empty case
all([]) returns True and any([]) returns False. This can cause silent bugs if the list is unexpectedly empty.

Summary at a Glance

Scenario any() all()
[True, True, True] True True
[True, False, True] True False
[False, False, False] False False
[] (empty) False True
Short-circuits on First True First False
Best used with Generator expression Generator expression

Question 1 of 5

What does any([0, "", None, False]) return?

Question 2 of 5

What does all([]) return, and why?

Question 3 of 5

Given nums = [-3, -1, 0, 2, 5], which elements does Python actually evaluate when running any(n > 0 for n in nums)?

Question 4 of 5

Why is any(x > 10 for x in data) preferred over any([x > 10 for x in data]) when data has millions of items?

Question 5 of 5

Which code correctly checks that every string in a list starts with a capital letter?