ReviseAlgo Logo

Built-in Functions

any and all

Testing boolean conditions across iterables

Interview: Common in interviews — clean validation patterns and short-circuit evaluation

Last Updated: June 12, 2026 7 min read

any() and all() are built-in functions that test boolean conditions across iterables. any() returns True if at least one element is truthy (like OR), while all() returns True if every element is truthy (like AND). Both support short-circuit evaluation for efficiency.

any(iterable)

  • Returns True: If ANY element is truthy — equivalent to chained OR
  • Short-circuits: Stops at the first truthy element — doesn't check the rest
  • Empty iterable: Returns False (no truthy elements exist)
  • With generator: any(x > 10 for x in nums) — memory-efficient lazy evaluation

all(iterable)

  • Returns True: If ALL elements are truthy — equivalent to chained AND
  • Short-circuits: Stops at the first falsy element — doesn't check the rest
  • Empty iterable: Returns True (vacuous truth — no falsy elements exist)
  • With generator: all(x > 0 for x in nums) — short-circuits on first non-positive

Use Generators, Not Lists

Always pass a generator expression, not a list comprehension: any(x > 0 for x in nums) (no brackets). This allows short-circuit evaluation — a list comprehension would evaluate ALL elements first, wasting time and memory.

Empty Iterable Behavior

any([]) = False and all([]) = True. This is mathematically correct (vacuous truth) but can be surprising. For validation, always check that the iterable is non-empty if that matters.

Use Cases

Input validation — checking that all fields meet requirements

Search operations — checking if any element matches criteria

Matrix/grid validation — all rows same length, all values in range

Security checks — any unauthorized access, all permissions granted

Data quality checks — all values non-null, any outliers present

Common Mistakes

Using list comprehension instead of generator — loses short-circuit benefit

Forgetting empty iterable behavior: any([]) = False, all([]) = True

Confusing any/all with OR/AND for individual values — use them for iterables

Not using generator expressions with large datasets — wastes memory on list creation

Assuming all() checks types — all([1, "", 3]) is False because "" is falsy