The most Pythonic and efficient way to check if all items in a list are true is with the built-in all() function. It returns True if every element is "truthy"—meaning it evaluates to True in a boolean context—and False otherwise.
# Example 1: All items are truthy
my_list = [1, "hello", [1, 2], True]
if all(my_list):
print("All items are true") # Output: All items are true
else:
print("Some items are false")
# Example 2: Some items are falsy
my_list = [1, "hello", 0, True]
if all(my_list):
print("All items are true")
else:
print("Some items are false") # Output: Some items are false
Understanding Python's "truthiness"
The all() function's behavior depends on Python's concept of "truthiness," where various data types are considered True or False in a boolean context.
Falsy values include:
NoneFalse- Numeric zero (
0,0.0,0j) - Empty sequences or collections (
"",(),[],{},set()) range(0)All other values are considered truthy. For instance, a non-zero number, a non-empty string, or a non-empty list will evaluate toTrue.
Methods for checking list items
Method 1: Using all() (The Pythonic way)
This is the most readable and efficient approach. It employs a short-circuiting mechanism, which means it stops and returns False as soon as it encounters the first falsy item, without checking the rest of the list.
Simple truthiness check:
Just pass the list directly to all() to check for general truthiness.
print(all([1, 2, 3])) # True
print(all([1, 0, 3])) # False
print(all(["a", "b", "c"])) # True
print(all(["a", "", "c"])) # False (empty string is falsy)
print(all([])) # True (an empty list has no falsy items)
Conditional checks with a generator expression:
To check if all items satisfy a specific condition, combine all() with a generator expression. This is far more memory-efficient than a list comprehension because it does not create a new list in memory.
numbers = [1, 5, 8, 12]
# Check if all numbers are greater than 0
all_positive = all(x > 0 for x in numbers)
print(all_positive) # True
# Check if all numbers are even
all_even = all(x % 2 == 0 for x in numbers)
print(all_even) # False
Method 2: Using a for loop
For a more explicit, step-by-step approach, you can write a manual for loop. Like all(), this method also uses a short-circuiting pattern.
def all_items_are_true(input_list):
for item in input_list:
if not item: # If an item is falsy...
return False # ...return False immediately
return True # If the loop finishes, all items were true
my_list_1 = [1, 2, 3]
print(all_items_are_true(my_list_1)) # True
my_list_2 = [1, 0, 3]
print(all_items_are_true(my_list_2)) # False
Method 3: Comparing set length
This method is suitable for checking if every element in a list is the literal True boolean, especially if the list can contain duplicates.
# Create a list with only boolean True values
bool_list_1 = [True, True, True]
# The set will contain only one element: {True}
if len(set(bool_list_1)) == 1 and True in set(bool_list_1):
print("All items are True") # Output: All items are True
# Create a list with a mix of boolean values
bool_list_2 = [True, True, False]
if len(set(bool_list_2)) == 1 and True in set(bool_list_2):
print("All items are True")
else:
print("Not all items are True") # Output: Not all items are True
Important: This approach is not suitable for general truthiness checks. For example, [1, 1, 1] would return True even though the items are integers, not booleans.
Method 4: Using len() with a list comprehension
You can create a list of only the falsy values and then check if the resulting list is empty. This is not short-circuiting and will process the entire list, making it less efficient for large lists.
my_list = [1, 2, 0, 4]
if len([x for x in my_list if not x]) == 0:
print("All items are true")
else:
print("Some items are false") # Output: Some items are false
Use code with caution.
Choosing the right method
- For most use cases, use
all(). It is the most readable and efficient solution, especially when combined with a generator expression for conditional checks. - Use a
forloop if you need to perform additional actions inside the loop (e.g., printing or logging) or if you want to be extremely explicit about the control flow. - Use
setcomparison only when you need to specifically verify that every item is the literal booleanTrue, and not just truthy. - Avoid
len()with a list comprehension for large lists, as it is less efficient thanall()due to not short-circuiting.