PythonBuilt-in functions
all()
Checks whether every item in a collection is truthy.
- Returns
TrueorFalse.
Example
#Python
tasks = [{"done": True}, {"done": True}, {"done": False}]
print(all(task["done"] for task in tasks))
print(all(task["done"] for task in tasks[:2]))
print(all([]))Resultsaved result, you can check it
False
True
True
Definition and usage
#The all() function returns True if every item is truthy. It stops at the first falsy item and returns False right away.
For an empty collection it returns True. That's consistent with the logic, but can be surprising, so it's worth checking the length separately when the data might be empty.
It is useful for validation: all(field.strip() for field in form_values) checks whether every form field is filled in.
Syntax
#Syntax
all(iterable)Parameters
#iterable
a collection
The items to check.
Good practices
#- Watch out for empty data:
all([])returnsTrue. - Combine
all()with a generator to check a condition on every item without building a list.
Related entries
#Spotted a mistake or missing an example? Write to us.