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.

</>
Copy
# 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:

  1. numbers is a list containing even integers.
  2. all() checks if each number satisfies the condition num % 2 == 0, meaning the number is even.
  3. The expression inside all() is a generator that iterates over each number in the list.
  4. If all numbers are even, all_even is True, otherwise, it’s False.

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.

</>
Copy
# 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:

  1. numbers contains positive integers.
  2. The for loop checks if each number is greater than 0.
  3. If a number is found that is less than or equal to 0, all_positive is set to False and the loop exits early.
  4. Otherwise, all_positive remains True.

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.

</>
Copy
# 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:

  1. words contains three strings.
  2. The generator expression inside all() checks if the letter “a” exists in each word.
  3. 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.

</>
Copy
# 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:

  1. elements contains integers.
  2. all() checks if each element in elements is an instance of int using isinstance().
  3. 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:

  1. all(): The most efficient method for checking conditions.
  2. for loop: A manual way to check each element.
  3. String conditions: "substring" in element inside all().
  4. isinstance(): Useful for checking data types.

The all() function is recommended for most use cases due to its simplicity and efficiency.