🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

any()

AI & DATA SCIENCE // any

any() returns True if at least one element of an iterable is truthy, and False if the iterable is empty or every element is falsy.

Syntax

any(iterable)

Deep Dive Course

any() short-circuits: it scans the iterable and returns True the moment it finds a truthy element, without evaluating the rest, which matters if you pass it a generator expression that's expensive to compute. On an empty iterable, any() returns False, matching the intuition that 'at least one is true' can't hold if there's nothing to check.

1Understanding any()

any() short-circuits: it scans the iterable and returns True the moment it finds a truthy element, without evaluating the rest, which matters if you pass it a generator expression that's expensive to compute. On an empty iterable, any() returns False, matching the intuition that 'at least one is true' can't hold if there's nothing to check.

💡

Pass a generator expression, not a list comprehension, to any() — it lets any() short-circuit without first building the whole list in memory.

editor.html
ages = [12, 15, 8, 20]
has_adult = any(age >= 18 for age in ages)
print(has_adult)
localhost:3000

2Practical Example

Here is a real-world application of any() showing how it is used in production Python code.

editor.html
errors = []
if any(errors):
    print("Something went wrong")
else:
    print("All checks passed")
localhost:3000

3Best Practices

Follow these guidelines when working with any():

1. Use any() with a generator expression instead of a manual loop with a flag variable and break

2. Prefer a generator expression over a list comprehension inside any()/all() so short-circuiting actually saves work

3. Remember any() on an empty iterable is False — check for that edge case if an empty input is possible

⚠️

Tip: Pass a generator expression, not a list comprehension, to any() — it lets any() short-circuit without first building the whole list in memory.

editor.html
ages = [12, 15, 8, 20]
has_adult = any(age >= 18 for age in ages)
print(has_adult)
localhost:3000

Examples

Example 01Basic Usage
ages = [12, 15, 8, 20]
has_adult = any(age >= 18 for age in ages)
print(has_adult)
Example 02Advanced Example
errors = []
if any(errors):
    print("Something went wrong")
else:
    print("All checks passed")

Best Practices

  • Use any() with a generator expression instead of a manual loop with a flag variable and break
  • Prefer a generator expression over a list comprehension inside any()/all() so short-circuiting actually saves work
  • Remember any() on an empty iterable is False — check for that edge case if an empty input is possible

Interview Question

Why is passing a generator expression to any() generally more efficient than passing a list comprehension?

Hint: Think about eager vs. lazy evaluation and short-circuiting.

The generator expression version produces values lazily, one at a time, so any() can stop as soon as it finds the first truthy result, short-circuiting. The list comprehension version builds the entire list first, evaluating the condition for every element up front, before any() even starts checking it, wasting time and memory if a match appears early or the list is large.

Exercises

MediumPractice using any() in a real scenario.
View Solution
ages = [12, 15, 8, 20]
has_adult = any(age >= 18 for age in ages)
print(has_adult)

Frequently Asked Questions

Why is passing a generator expression to any() generally more efficient than passing a list comprehension?

The generator expression version produces values lazily, one at a time, so any() can stop as soon as it finds the first truthy result, short-circuiting. The list comprehension version builds the entire list first, evaluating the condition for every element up front, before any() even starts checking it, wasting time and memory if a match appears early or the list is large.

Related Functions

all()filter()generator expressions