Python any() and all() Functions: Check If Any or All Items Are True

Learn how Python any() and all() work with lists, strings, dictionaries, truthy and falsy values, generator expressions, short-circuiting, and common mistakes.

Published

Updated

Read time 7 min read

Reviewed byDeepak Prasad

Python any() and all() Functions: Check If Any or All Items Are True

any() and all() answer two common questions about an iterable: “Is at least one item truthy?” and “Are all items truthy?” They are built-ins—no import needed—and they short-circuit, so they often replace manual loops for simple checks. Pair them with generator expressions (parentheses instead of square brackets) so work stops as soon as the answer is known.

For truthiness in branches, see Python if else. For comprehension-style syntax, see list comprehension—the same expression forms appear in generator expressions used with any / all.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


What does any() do in Python?

any(iterable) returns True if at least one element of iterable is true in a Boolean context, otherwise False. On an empty iterable it returns False (there is no “witness”).

python
print(any([0, "", None, 1]))
print(any([0, "", None]))
print(any([]))
Output

The first line prints True because 1 is truthy; the second prints False; the third prints False for the empty list.


What does all() do in Python?

all(iterable) returns True only if every element is truthy. If any element is falsy, it returns False. On an empty iterable it returns True (vacuous truth: there is no counterexample).

python
print(all([1, 2, 3]))
print(all([1, 0, 3]))
print(all([]))
Output

Prints True, then False, then True.


any() vs all() in Python

Think of any as a big OR over the truthiness of items, and all as a big AND. That analogy breaks only for the empty iterable: any has nothing true to latch onto, so it is False, while all has nothing false to disprove, so it is True.

any(iterable) all(iterable)
Question Exists a truthy item? Are all items truthy?
Empty input False True
Stops when First truthy value found First falsy value found

The same list can answer both questions differently:

python
samples = [1, 0, 0, 0]
print(any(samples), all(samples))

samples2 = [1, 2, 3]
print(any(samples2), all(samples2))
Output

The first line prints True False (something is non-zero, but not every cell is). The second prints True True (every value is truthy here). Use any when you care about existence (“do we have at least one pass?”) and all when you need a global pass (“did every check succeed?”).


Python any() and all() syntax and return value

Both functions take exactly one positional argument: an iterable. Each returns a plain bool. They are not variadic: in Python 3, any(1, 2, 3) raises TypeError because extra positional arguments are not allowed—wrap tests in a list, tuple, or generator if you have several bare values.

python
print(any([False, False, True]))
try:
    any(0, 1)
except TypeError as e:
    print("error:", e)
Output

Iteration follows normal rules: for a dict, the iterable’s items are the keys (covered again below). For a generator or file-like iterator, the function consumes elements until it can decide the result, so you cannot “rewind” the stream afterward.


Truthy and falsy values used by any() and all()

Internally, any and all apply truth-value testing (like bool(x) on each element). Falsy values include 0, 0.0, None, False, "", and empty built-in containers such as [], {}, and set(). Most other objects are truthy in Python, including non-empty strings and non-zero numbers—so any("no") is True because the string is non-empty, even if the text means “negative.”

python
print(any([0, None, ""]))
print(all([1, "ok", [0]]))

class AlwaysFalse:
    def __bool__(self):
        return False

print(any([AlwaysFalse(), 1]))
Output

The first line is False (every item is falsy). The second is True (each element is truthy; the inner list [0] is a non-empty object, so it is truthy as a value). The third line is True because 1 appears after a custom falsy object—any stops at the first truthy value, but here it must inspect until it finds 1.


any() with lists, tuples, sets, and strings

Behavior is the same for any iterable: each element is tested.

python
print(any([0, 0, 2]))
print(any((0, 0, 0)))
print(any({0, 0, 0}))
print(any("abc"))
print(any("   "))
Output

The first line is True because 2 is truthy. The next two lines are False because every element is 0 (the set literal collapses to {0}). For strings, any walks characters: "abc" is True because each letter is truthy; a string of only spaces is still True because ordinary space characters are non-empty and truthy.


any() with dictionaries

Important: any(d) and all(d) iterate keys only, not values.

python
d = {0: "yes", 1: "no"}
print(any(d), all(d))

d2 = {"a": 0, "b": 0}
print(any(d2.values()))
Output

First line: key 0 is falsy, key 1 is truthy, so any(d) is True and all(d) is False. Second: testing values with .values() returns False here because both stored values are zero. For more on mappings, see Python dictionary.


Use any() with generator expressions

Prefer a generator expression (expr for x in it) over [expr for x in it] when you only need any / all: nothing materializes the whole list of results, and short-circuiting can bail out early.

python
nums = range(1_000_000)
print(any(n > 5 for n in nums))
Output

That finds a witness quickly without building a million Booleans. The same idea appears in list comprehension syntax—swap [] for () to get a generator.


Check if a string contains any digit or letter

Use character tests inside a generator. For substring ideas without per-character scans, see Python string contains substring.

python
s1 = "room 42b"
s2 = "no digits here"
print(any(ch.isdigit() for ch in s1))
print(any(ch.isdigit() for ch in s2))
print(any(ch.isalpha() for ch in "123"))
Output

Prints True, False, and False for the third ("123" has no letters).


Use any() for multiple conditions

any takes one iterable. Combine conditions in a tuple or generator—do not pass multiple separate arguments (any(a, b) raises TypeError in Python 3).

python
x = 7
print(any([x < 0, x > 10, x == 7]))
print(any((x < 0, x > 10, x == 7)))
Output

Both print True. For branching style, spell out multi-branch logic with if / elif; any/all keep OR/AND of Booleans compact.


Short-circuit behavior of any() and all()

any stops at the first truthy value and returns True immediately. all stops at the first falsy value and returns False immediately. Remaining items are not evaluated—important for generators with side effects or expensive predicates.

python
def noisy(n):
    print("check", n)
    return n > 0

print(any(noisy(i) for i in [0, 0, 3, 4]))
Output

You see check 0, check 0, check 3, then Truenoisy is never called with 4.

python
def noisy_pos(n):
    print("all check", n)
    return n > 0

print(all(noisy_pos(i) for i in [1, 2, 0, 4]))
Output

all stops at the first falsy input: you see all check 1, all check 2, all check 0, then False—it never reaches 4.


Common mistakes with any() and all()

  • Calling any(a, b, c) instead of any([a, b, c])any accepts only one iterable argument.
  • Forgetting that any(dict) tests keys, not values—use .values() or .items() when the stored data matters.
  • Building [pred(x) for x in huge] before any(...)—use (pred(x) for x in huge) so you do not allocate a giant list.
  • Expecting all([]) to be False—empty means True by definition.

Python any() and all() quick reference table

Call Result pattern
any(it) True if some item truthy; False if empty or all falsy
all(it) True if empty or every item truthy; False if any falsy
any(d) / all(d) Tests keys of dict d
any(f(x) for x in it) Lazy; stops on first f(x) truthy
all(f(x) for x in it) Lazy; stops on first f(x) falsy

Summary

any and all turn iterable truthiness into a single Boolean: any behaves like a short-circuiting OR with False on empty input; all behaves like short-circuiting AND with True on empty input. On dictionaries, both walk keys unless you pass .values() or .items(). Generator expressions pair naturally with both builtins so you avoid building full flag lists and you stop as soon as the answer is known—clearer and often faster than ad-hoc loops for simple existence or “every” checks.


References


Frequently Asked Questions

1. What does Python any() return on an empty iterable?

any(()) and any([]) return False because there is no truthy element to find.

2. What does Python all() return on an empty iterable?

all(()) and all([]) return True—vacuous truth: every element in the empty set satisfies “is truthy” trivially.

3. Does any(my_dict) look at keys or values?

any(d) and all(d) iterate the dictionary’s keys only; use d.values() or d.items() when you need to test stored values.

4. Why use a generator expression with any() instead of a list comprehension?

A generator feeds items lazily so any() can stop at the first truthy value without building a full list of flags first, which saves memory and work on large iterables.

5. How are any() and all() different from a for loop?

They express “exists” or “for all” tests in one call and short-circuit; a loop is clearer when you also need the index, side effects on each step, or more than a boolean result.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …