Python all() Method
In this tutorial, we will learn about the python all() method and how we can use it.
Python’s all() function tests whether every element in an iterable is truthy. It accepts a single argument – any iterable such as a list, tuple, set, dictionary, or generator – and returns True only if all elements evaluate to True under Python’s standard truth-testing rules. If even one element is falsy (such as 0, False, None, or an empty string), all() returns False. Notably, all() returns True for an empty iterable, since there are no elements that could be false. This function is the logical counterpart of Python any(), which returns True if at least one element is truthy. all() is widely used for data validation (ensuring all fields pass a check), precondition testing (verifying all requirements are met before proceeding), and filtering logic. It short-circuits evaluation, meaning it stops iterating as soon as it encounters the first falsy value, making it efficient even on large iterables.
Syntax Breakdown
1
all(iterable)
all() accepts exactly one argument, which must be an iterable. It returns a bool – either True or False. Internally, all() is equivalent to:
1
2
3
4
5
def all(iterable):
for element in iterable:
if not element:
return False
return True
This means all() short-circuits: as soon as a falsy value is found, evaluation stops immediately without examining the remaining elements.
Python all() Parameters
The all() method will take a single argument as an iterable, including list, tuple, set, dictionary, string, and generator expressions in Python.
What does all() return?
The all() function returns True if every element in the iterable is truthy, or if the iterable is empty. It returns False as soon as any falsy element is found.
When should you use all()?
Use all() when you need to confirm that every item in a collection meets a condition. Common scenarios include validating that all form fields are filled in, checking that all test cases pass, or verifying that all values in a dataset are within an expected range.
The all() is a built-in method available in Python, which will return True when all the elements in the given iterable are true. If any null value is present in the iterable, it will return False.
An empty iterable will return True in the all() function.
Code Examples
Example 1: How to use all() in lists?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
my_list = [1, 2, 3, 4, 5]
print(all(my_list)) # True – all elements are truthy
my_list = [0, False]
print(all(my_list)) # False – 0 is falsy
my_list = [0, 1, 2, 3, 4, 5]
print(all(my_list)) # False – 0 is falsy
my_list = [0, False, 1]
print(all(my_list)) # False – 0 is falsy
my_list = []
print(all(my_list)) # True – vacuously true for empty iterables
Output:
1
2
3
4
5
True
False
False
False
True
The all() method will work similarly for tuples and sets like the list in Python.
Example 2: How to use all() on strings?
When applied to a string, all() iterates over each character. An empty string returns True (empty iterable); a non-empty string returns True because individual characters are always truthy.
1
2
3
4
5
my_string = "This is Python"
print(all(my_string)) # True
my_string = ""
print(all(my_string)) # True – empty iterable
Output:
1
2
True
True
Example 3: How to use all() on dictionaries?
When applied to a dictionary, all() iterates over the keys (not values). Keys that evaluate to falsy (such as 0) cause all() to return False.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
my_dict = {1: True}
print(all(my_dict)) # True – key 1 is truthy
my_dict = {0: False}
print(all(my_dict)) # False – key 0 is falsy
my_dict = {0: 'False', 1: 'True'}
print(all(my_dict)) # False – key 0 is falsy
my_dict = {0: 'False'}
print(all(my_dict)) # False – key 0 is falsy
my_dict = {}
print(all(my_dict)) # True – empty dict
Output:
1
2
3
4
5
True
False
False
False
True
In dictionaries, all() checks whether all keys are true, not the values. To check values, use all(my_dict.values()).
Example 4: Using all() with a Generator Expression
Generator expressions are the most powerful way to use all(), combining a condition with iteration in a single readable line:
1
2
3
4
5
6
7
numbers = [2, 4, 6, 8, 10]
all_even = all(n % 2 == 0 for n in numbers)
print("All even?", all_even) # True
scores = [85, 92, 78, 45, 99]
all_passing = all(score >= 50 for score in scores)
print("All passing?", all_passing) # False – 45 fails
Example 5: Validating Required Form Fields
1
2
3
4
5
def is_form_valid(name, email, phone):
return all([name.strip(), email.strip(), phone.strip()])
print(is_form_valid("Alice", "[email protected]", "555-1234")) # True
print(is_form_valid("Alice", "", "555-1234")) # False
Real-World Use Cases
Validating that all required form fields are filled
When processing user input with multiple fields, all([name, email, password]) quickly checks that none of the required values are empty or None before proceeding with further validation.
Checking preconditions before executing a task
In automation scripts or pipelines, you can verify that all required conditions are met:
1
2
3
4
5
6
7
8
file_exists = True
has_permissions = True
disk_space_ok = True
if all([file_exists, has_permissions, disk_space_ok]):
print("Environment is ready. Starting operation.")
else:
print("Preconditions not met. Aborting.")
Ensuring data quality in a dataset
1
2
3
4
5
6
7
dataset = [
{'name': 'Alice', 'score': 92},
{'name': 'Bob', 'score': 78},
{'name': 'Carol', 'score': -5},
]
valid = all(row['score'] > 0 for row in dataset)
print("All scores valid:", valid) # False
Checking all required permissions
1
2
3
4
5
required_permissions = {'read', 'write', 'execute'}
user_permissions = {'read', 'write', 'execute', 'delete'}
has_access = all(p in user_permissions for p in required_permissions)
print("Access granted:", has_access) # True
Edge Cases and Gotchas
Empty iterables always return True: This is mathematically correct (vacuous truth) but can be surprising. If you want to reject empty inputs, add an explicit check: bool(my_list) and all(my_list).
Dictionary iteration uses keys, not values: A very common mistake is expecting all() to check dictionary values. Use all(my_dict.values()) to check values.
Generator expressions are consumed once: If you pass a generator to all(), it will be exhausted afterward and cannot be reused.
Short-circuit behavior with side effects: Because all() stops at the first falsy value, functions with side effects inside a generator expression may not all be called. Avoid side effects inside all().
Comparison: all() vs a for-loop
1
2
3
4
5
6
7
8
9
# Using all() – concise and Pythonic
result = all(x > 0 for x in numbers)
# Equivalent for-loop
result = True
for x in numbers:
if not x > 0:
result = False
break
Both short-circuit on the first falsy value, but all() is far more readable.
Frequently Asked Questions
Q: Does all() work with generators? A: Yes. all() accepts any iterable, including generators and generator expressions.
Q: What does all() return for an empty list? A: True. This is called vacuous truth – there are no elements to violate the condition.
Q: Does all() check dictionary keys or values? A: Keys. Use all(d.values()) to check values.
Q: Is all() faster than a for-loop? A: all() short-circuits just like a for-loop with a break. Performance is similar; all() wins on readability.
Related Functions
- Python any() – return
Trueif at least one element is truthy. - Python bool() – convert a single value to Boolean.
- Python len() – count the number of elements in an iterable.
