Check if any element in list satisfies a condition-Python
Last Updated :
11 Feb, 2025
The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the condition.
Using any()
any() is the most efficient way to check if any element in a list satisfies a condition. It short-circuits, meaning it stops checking as soon as it finds a match, making it ideal for performance-sensitive tasks.
Python
a = [4, 5, 8, 9, 10, 17]
res = any(ele > 10 for ele in a)
print(res)
Explanation: any() iterates through the list and checks if any element is greater than 10. It stops as soon as it finds the first matching element.
Using next()
next() allows us to retrieve the first matching element from a generator. When used with a conditional generator expression, it can efficiently check for the presence of an element that satisfies a condition.
Python
a = [4, 5, 8, 9, 10, 17]
res = next((True for ele in a if ele > 10), False)
print(res)
Explanation: next() iterates through the list and checks if any element is greater than 10. It stops as soon as it finds the first matching element and returns True otherwise, it returns False.
Using filter()
filter() applies a condition to each element and returns an iterator of matching values. However, converting this iterator to a list just to check if it has elements makes it inefficient for this use case.
Python
a = [4, 5, 8, 9, 10, 17]
res = bool(list(filter(lambda x: x > 10, a)))
print(res)
Explanation: filter() extracts elements greater than 10, converts them into a list and bool() checks if the list is non-empty. It does not short-circuit.
Using loop
A traditional for loop can manually check each element and break as soon as a match is found. While effective, it is less readable than any().
Python
a = [4, 5, 8, 9, 10, 17]
res = False
for ele in a:
if ele > 10:
res = True
break
print(res)
Explanation: for loop iterates through the list and checks if any element is greater than 10. It sets res to True and breaks the loop as soon as it finds the first matching element.
Similar Reads
Python | Check if all elements in list follow a condition Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using
4 min read
Python - Check if List contains elements in Range Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function -
3 min read
Python | Check if two lists have any element in common Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check.Using set IntersectionSet intersection uses Python's
3 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python Check if the List Contains Elements of another List The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False. Using
3 min read