Built-in function

all()

Returns True when every item in a list or other iterable is true, and False as soon as one item is false. An empty iterable gives True.

BeginnerEvery example below runs in your browser. Change the code and press Run.

all(iterable)

all() goes through a collection and returns True only if every single item is true. The moment it meets a false item it stops and returns False.

Use it to validate data in one line: are all the scores between 0 and 100, are all the fields filled in, does every file name end in .csv? A generator expression inside the brackets holds the test.

The surprise: all([]) is True. With no items there is nothing that fails the test, so the answer is yes. If an empty list should count as a failure, check if items and all(...).

Parameters

ParameterWhat it is
iterable
required
Any collection you can loop over: a list, tuple, set, string, or a generator expression.

Return value

True if every item is truthy (or the iterable is empty), otherwise False.

Examples

1Are all values true?

Pythoneditable
print(all([True, True, True]))
print(all([1, 2, 0, 4]))
print(all(["a", "b", ""]))
Output
True
False
False

0 and the empty string "" count as false.

2Validate exam scores

Pythoneditable
scores = [78, 92, 64, 100]
print(all(0 <= s <= 100 for s in scores))
scores.append(105)
print(all(0 <= s <= 100 for s in scores))
Output
True
False

3Check that a form is filled in

Pythoneditable
form = {"name": "Priya", "email": "priya@example.com", "city": ""}
if all(form.values()):
    print("Form complete")
else:
    missing = [key for key, value in form.items() if not value]
    print("Missing:", missing)
Output
Missing: ['city']

4Every file has the right extension

Pythoneditable
files = ["jan.csv", "feb.csv", "mar.CSV"]
print(all(f.endswith(".csv") for f in files))
print(all(f.lower().endswith(".csv") for f in files))
Output
False
True

5The empty list case

Pythoneditable
tasks_done = []
print(all(tasks_done))
print(bool(tasks_done) and all(tasks_done))
Output
True
False

Add bool(items) and (or items and) when an empty list should not pass.

Common mistakes

Forgetting that all([]) is True

Code like if all(x > 0 for x in numbers): print("all positive") says "all positive" for an empty list too. If that is wrong for your program, test that the list has items first.

Pythoneditable
numbers = []
print(all(x > 0 for x in numbers))
print(len(numbers) > 0 and all(x > 0 for x in numbers))
Output
True
False

Checking strings when you meant to check numbers

Values read from a file or input() are strings, and "0" is a non-empty string, so it counts as true. Convert first: all(int(v) for v in values).

Pythoneditable
values = ["3", "0", "7"]
print(all(values))
print(all(int(v) for v in values))
Output
True
False

Questions people ask

Why does all() return True for an empty list?

Because no item fails the test. Logicians call this "vacuous truth". Use bool(items) and all(items) if empty should mean False.

How do I check if all items in a list are the same?

Compare each with the first: all(x == items[0] for x in items), or len(set(items)) <= 1 when the items are hashable.

How do I check if all items are numbers?

all(isinstance(x, (int, float)) for x in items) uses isinstance() on each item.

What is the opposite of all()?

not all(...) means "at least one fails". If you want "at least one passes", use any().

Spotted a mistake on this page? Tell us