Check if any element in list meets a condition in Python

avatar

Borislav Hadzhiev

Last updated: Aug 10, 2022

banner

Photo from Unsplash

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

Use the any() function to check if any element in a list meets a condition, e.g. if any(item > 10 for item in my_list):. 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))

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.

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')

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')

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.

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.