Python
any function
programming
coding
software development

How does this input work with the Python 'any' function?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python's any() is small, but it causes confusion because it works on truthiness, not on literal True values. It also short-circuits, so if the input is a generator or another one-pass iterable, only part of it may be consumed.

What any() Actually Checks

any(iterable) returns True if at least one element of the iterable is truthy. If every element is falsy, the result is False.

python
print(any([False, False, True]))
print(any([0, 0, 0]))
print(any([]))

The output is:

python
True
False
False

This behavior is not about the object being literally equal to True. It is about whether the object behaves as true in a boolean context.

Truthiness Explains the Surprising Cases

In Python, many values are falsy:

  • 'False'
  • 'None'
  • '0'
  • '0.0'
  • '""'
  • '[]'
  • '{}'

Most other values are truthy, including non-empty strings and nonzero numbers.

python
print(any(["", "hello"]))     # True
print(any([None, 0, []]))     # False
print(any(["false"]))         # True

That last case surprises people often. The string "false" is non-empty, so it is truthy. any() does not inspect the word and try to interpret its meaning.

any() Short-Circuits

any() stops at the first truthy element. That makes it efficient, but it also means it may consume only part of a generator.

python
1def is_even(n):
2    print(f"checking {n}")
3    return n % 2 == 0
4
5
6numbers = [1, 3, 7, 10, 11]
7result = any(is_even(n) for n in numbers)
8print(result)

The function stops once it reaches 10, because that is the first value for which is_even returns True. The final 11 is never checked.

This matters whenever the iterable has side effects or cannot easily be reused.

Dictionaries Are a Special Case to Notice

Iterating a dictionary yields keys, not values. That means any(my_dict) is checking the truthiness of the keys.

python
1d = {"a": 0, "b": 0}
2
3print(any(d))
4print(any(d.values()))

The first call returns True because "a" and "b" are non-empty strings. The second returns False because both values are zero. If your intention is to inspect values, call .values() explicitly.

Prefer Generator Expressions Over Lists

When using any() with a condition, a generator expression is usually better than building a list first.

python
result = any(n > 5 for n in range(10))
print(result)

This is preferable to:

python
result = any([n > 5 for n in range(10)])
print(result)

The generator version avoids creating an intermediate list and preserves short-circuit behavior more naturally.

any() Is Different from all()

any() asks whether at least one element passes. all() asks whether every element passes.

python
1values = [2, 4, 6, 7]
2
3print(any(v % 2 != 0 for v in values))
4print(all(v % 2 == 0 for v in values))

Knowing both functions makes validation and filtering code much clearer than writing manual loops with flags.

Common Pitfalls

  • Expecting any() to search for the literal object True instead of using truthiness.
  • Passing a dictionary and forgetting that iteration checks keys by default.
  • Reusing a generator after any() has already consumed part of it.
  • Building an unnecessary list comprehension instead of using a generator expression.
  • Treating any([]) as a Python bug instead of understanding that empty input simply means no truthy element was found.

Summary

  • 'any() returns True when at least one element of an iterable is truthy.'
  • It uses truthiness rules, not semantic parsing of strings or literal equality to True.
  • It short-circuits on the first truthy element.
  • On dictionaries, it checks keys unless you ask for .values() or .items().
  • Generator expressions are usually the best input form for any().

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.