Check if all/any elements in List meet condition in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Check if ANY element in a List meets a condition in Python
  2. Check if ALL elements in a List meet a condition in Python

# Check if ANY element in a list meets a condition in Python

Use the any() function to check if any element in a list meets a condition.

The any function will return True if any element in the list meets the condition and False otherwise.

main.py
my_list = [1, 3, 5, 15] if any(item > 10 for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('There is an item greater than 10') else: print('No items in the list are greater than 10') # ๐Ÿ‘‡๏ธ True print(any(item > 10 for item in my_list)) # ๐Ÿ‘‡๏ธ False print(any(item > 50 for item in my_list))

check if any element in list meets a condition

The code for this article is available on GitHub

If you need to check if ALL elements in a list meet a condition, click on the following subheading:

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

We passed a generator expression to the any() function.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

In the example, we check if each item in the list is greater than 10 and return the result.

# Getting the element that meets the condition

You can use the assignment expression syntax if you need to get the element that meets the condition.

main.py
my_list = [1, 3, 5, 15] if any((match := item) > 10 for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('There is an item greater than 10') print(match) # ๐Ÿ‘‰๏ธ 15 else: print('No items in the list are greater than 10')

getting the element that meets the condition

The code for this article is available on GitHub

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

If the condition is met at least once, the any() function returns True.

If the iterable we pass to the any()function is empty or none of the elements in the iterable are truthy, the any function returns False.
main.py
my_list = [1, 3, 5] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # ๐Ÿ‘‡๏ธ this runs print('No items in the list are greater than 10')

None of the items in the list is greater than 10, so the condition is never met and any() returns False.

If your list is empty, the any function will always return False.

main.py
my_list = [] if any(item > 10 for item in my_list): print('There is an item greater than 10') else: # ๐Ÿ‘‡๏ธ this runs print('No items in the list are greater than 10')
The code for this article is available on GitHub

Here is another example that uses the any() function to check if at least 1 item in the list has a None value.

main.py
my_list = ['a', 'b', None, 'd'] if any(item is None for item in my_list): # ๐Ÿ‘‡๏ธ This runs print('There is a None value in the list') else: print('No items in the list have a value of None') # ๐Ÿ‘‡๏ธ True print(any(item is None for item in my_list))

The generator expression we passed to the any() function iterates over the list and checks if each value is None.

If the condition is met at least once, the any function returns True, otherwise, it returns False.

# Check if ANY element in a list meets a condition using a for loop

You can also use a for loop to check if any element in a list meets a condition.

main.py
my_list = [1, 3, -4, 5, 15, -3] any_meets_condition = False for element in my_list: if element > 10: any_meets_condition = True print(element) # ๐Ÿ‘‰๏ธ 15 break print(any_meets_condition) # ๐Ÿ‘‰๏ธ True

check if any element in list meets condition using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the list

On each iteration, we check if the current element is greater than 10.

If the condition is met, we set the any_meets_condition variable to True and exit the loop.

You also have access to the element that meets the condition in the if block.

# Check if ALL elements in a List meet a condition in Python

Use the all() function to check if all elements in a list meet a condition.

The all function will return True if all elements in the list meet the condition and False otherwise.

main.py
my_list = [1, 3, 5, 15] if all(item > 0 for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('All elements in the list are greater than 0') else: print('Not all elements in the list are greater than 0') # ๐Ÿ‘‡๏ธ True print(all(item > 0 for item in my_list)) # ๐Ÿ‘‡๏ธ False print(all(item > 10 for item in my_list))

check if all elements in list meet condition

The code for this article is available on GitHub

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

We passed a generator expression to the all() function.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

In the example, we check if all elements in the list are greater than 0.

The all function will return True if all elements in the list meet the condition and False otherwise.

If a single value that doesn't meet the condition is encountered, the all() function short-circuits returning False.

# Check if ALL elements in a List meet a condition using a for loop

You can also use a basic for loop to check if all elements in a list meet a condition.

main.py
my_list = [1, 3, 5, 15] all_meet_condition = True for element in my_list: if element < 0: all_meet_condition = False break print(all_meet_condition) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

We initialized a variable to True and used a for loop to iterate over the list.

On each iteration, we check if the current item is less than 0.

If the condition is met, we set the all_meet_condition variable to False and break out of the loop.

If the condition is never met, the all_meet_condition variable remains set to True.

The break statement breaks out of the innermost enclosing for or while loop.

You can also use this approach to get the elements that don't meet the condition or to get the ones that do.

main.py
my_list = [1, 3, -4, 5, 15, -3] all_meet_condition = True meet_condition = [] dont_meet_condition = [] for element in my_list: if element < 0: all_meet_condition = False dont_meet_condition.append(element) else: meet_condition.append(element) print(all_meet_condition) # ๐Ÿ‘‰๏ธ False print(meet_condition) # ๐Ÿ‘‰๏ธ [1, 3, 5, 15] print(dont_meet_condition) # ๐Ÿ‘‰๏ธ [-4, -3]
The code for this article is available on GitHub

We used a for loop to iterate over the list.

On each iteration, we check if the current element is less than 0.

If the condition is met, we append the value to the dont_meet_condition list.

Otherwise, the value is appended to the meet_condition list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev