Python 3 reference · Built-in Functions
Python all() Function
Return True if every item in an iterable is truthy, or if the iterable is empty. Stops at the first falsy item.
Python 3 Since 3.0
Syntax
all(iterable)
Parameters
| Parameter | Description |
|---|---|
iterable |
Any iterable: a list, tuple, set, generator, … |
Return value
True if all items are truthy (or there are no items), otherwise False.
Examples
print(all([True, 1, "yes"]))
print(all([True, 0, "yes"]))
print(all([])) # empty: nothing is false
Output
True False True
scores = [72, 88, 95, 61]
print(all(s >= 60 for s in scores)) # did everyone pass?
print(all(s >= 70 for s in scores))
Output
True False
password = "Secret123"
checks = [len(password) >= 8, any(c.isdigit() for c in password), any(c.isupper() for c in password)]
print("strong" if all(checks) else "weak")
Output
strong
Common mistakes
all([])isTrue. If an empty list should count as a failure, checkif items and all(...).- Pass a generator expression (
all(x > 0 for x in nums)) rather than a list comprehension, soall()can stop as soon as it finds a false item.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
any()· Return True if at least one item in an iterable is truthy. Returns False for an empty iterable and stops at the first truthy item.filter()· Keep only the items of an iterable for which a function returns True. Returns a lazy iterator, not a list.bool()· Convert a value to True or False using Python's truth rules: zero, empty and None are False, everything else is True.- Python Booleans · tutorial
- Python List Methods and Comprehensions · tutorial