🚀 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...

all()

AI & DATA SCIENCE // all

all() returns True if every element of an iterable is truthy (or the iterable is empty), and False as soon as one element is falsy.

Syntax

all(iterable)

Deep Dive Course

all() is the logical counterpart to any(): it short-circuits and returns False the instant it finds a falsy element, without checking the rest. On an empty iterable it returns True, vacuously, since there are no elements that could violate the 'all are true' condition — the standard mathematical convention that an empty conjunction is true.

1Understanding all()

all() is the logical counterpart to any(): it short-circuits and returns False the instant it finds a falsy element, without checking the rest. On an empty iterable it returns True, vacuously, since there are no elements that could violate the 'all are true' condition — the standard mathematical convention that an empty conjunction is true.

💡

An empty iterable being considered 'all true' trips people up — double check whether an empty input should really count as every condition being satisfied in your specific logic.

editor.html
ages = [22, 25, 19, 30]
all_adults = all(age >= 18 for age in ages)
print(all_adults)
localhost:3000

2Practical Example

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

editor.html
form_fields = {"name": "Alice", "email": "a@x.com", "phone": ""}
is_complete = all(form_fields.values())
print(is_complete)
localhost:3000

3Best Practices

Follow these guidelines when working with all():

1. Use all() with a generator expression instead of a manual loop with a flag variable to check that every item passes a check

2. Combine all() with a generator expression, not a list comprehension, so it can short-circuit on the first failure

3. Explicitly handle the empty-iterable case if the default vacuous-true behavior isn't what you want

⚠️

Tip: An empty iterable being considered 'all true' trips people up — double check whether an empty input should really count as every condition being satisfied in your specific logic.

editor.html
ages = [22, 25, 19, 30]
all_adults = all(age >= 18 for age in ages)
print(all_adults)
localhost:3000

Examples

Example 01Basic Usage
ages = [22, 25, 19, 30]
all_adults = all(age >= 18 for age in ages)
print(all_adults)
Example 02Advanced Example
form_fields = {"name": "Alice", "email": "a@x.com", "phone": ""}
is_complete = all(form_fields.values())
print(is_complete)

Best Practices

  • Use all() with a generator expression instead of a manual loop with a flag variable to check that every item passes a check
  • Combine all() with a generator expression, not a list comprehension, so it can short-circuit on the first failure
  • Explicitly handle the empty-iterable case if the default vacuous-true behavior isn't what you want

Interview Question

What does all() evaluate to when given an empty iterable, and why?

Hint: Think about the mathematical convention for an empty logical conjunction.

all() on an empty iterable is True. Logically, 'all elements satisfy X' is vacuously true when there are no elements to violate it, the same convention as an empty product equaling 1 or an empty sum equaling 0. This is a common source of subtle bugs when code assumes an empty collection should fail a validation check.

Exercises

MediumPractice using all() in a real scenario.
View Solution
ages = [22, 25, 19, 30]
all_adults = all(age >= 18 for age in ages)
print(all_adults)

Frequently Asked Questions

What does all() evaluate to when given an empty iterable, and why?

all() on an empty iterable is True. Logically, 'all elements satisfy X' is vacuously true when there are no elements to violate it, the same convention as an empty product equaling 1 or an empty sum equaling 0. This is a common source of subtle bugs when code assumes an empty collection should fail a validation check.

Related Functions

any()filter()generator expressions