Check if All Elements in a List Satisfy a Condition in Python
In Python, we can check if all elements in a list satisfy a condition using the all()
function, list comprehensions, or loops. The all()
function is the most efficient way to check conditions across all elements in a list.
Examples
1. Check if All Elements in a List Satisfy a Condition using the all()
Function
The all()
function returns True
if all elements in an iterable evaluate to True
. This is the most Pythonic way to check conditions in a list.
# List of numbers
numbers = [2, 4, 6, 8, 10]
# Checking if all numbers are even
all_even = all(num % 2 == 0 for num in numbers)
# Printing the result
print("Are all numbers even?", all_even)
Explanation:
Here:
numbers
is a list containing even integers.all()
checks if each number satisfies the conditionnum % 2 == 0
, meaning the number is even.- The expression inside
all()
is a generator that iterates over each number in the list. - If all numbers are even,
all_even
isTrue
, otherwise, it’sFalse
.
Output:
Are all numbers even? True
2. Check if All Elements in a List Satisfy a Condition using a For Loop
A for
loop can be used to iterate over the list and manually check if all elements satisfy a condition.
# List of numbers
numbers = [5, 15, 25, 35]
# Checking if all numbers are greater than 0
all_positive = True
for num in numbers:
if num <= 0:
all_positive = False
break
# Printing the result
print("Are all numbers positive?", all_positive)
Explanation:
Here:
numbers
contains positive integers.- The
for
loop checks if each number is greater than 0. - If a number is found that is less than or equal to 0,
all_positive
is set toFalse
and the loop exits early. - Otherwise,
all_positive
remainsTrue
.
Output:
Are all numbers positive? True
3. Checking if All Strings Contain a Specific Character
We can use the all()
function to check if all strings in a list contain a specific character.
# List of strings
words = ["apple", "apricot", "avocado"]
# Checking if all words contain the letter 'a'
all_contain_a = all("a" in word for word in words)
# Printing the result
print("Do all words contain 'a'?", all_contain_a)
Explanation:
Here:
words
contains three strings.- The generator expression inside
all()
checks if the letter “a” exists in each word. - If every word contains “a”, the result is
True
.
Output:
Do all words contain 'a'? True
4. Checking if All Elements Are of the Same Data Type
We can use all()
with isinstance()
to check if all elements in a list belong to the same data type.
# List of elements
elements = [10, 20, 30, 40]
# Checking if all elements are integers
all_integers = all(isinstance(elem, int) for elem in elements)
# Printing the result
print("Are all elements integers?", all_integers)
Explanation:
Here:
elements
contains integers.all()
checks if each element inelements
is an instance ofint
usingisinstance()
.- If all elements are integers, the result is
True
.
Output:
Are all elements integers? True
Conclusion
To check if all elements in a list satisfy a condition in Python, we can use:
all()
: The most efficient method for checking conditions.for
loop: A manual way to check each element.- String conditions:
"substring" in element
insideall()
. isinstance()
: Useful for checking data types.
The all()
function is recommended for most use cases due to its simplicity and efficiency.