Python | Check if any element in list satisfies a condition
Last Updated :
01 Jun, 2023
Sometimes, while working with Python lists, we can have a problem to filter a list. One of the criteria of performing this filter operation can be checking if any element exists in list that satisfies a condition. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension This problem can be easily solved using loops. But this method provides a one liner to solve this problem. List comprehension just checks for any element that satisfies a condition.
Python3
test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]
print ( "The original list : " + str (test_list))
res = True in (ele > 10 for ele in test_list)
print ( "Does any element satisfy specified condition ? : " + str (res))
|
Output
The original list : [4, 5, 8, 9, 10, 17]
Does any element satisfy specified condition ? : True
Time complexity: O(n), n is length of list.
Auxiliary space: O(1)
Method #2 : Using any() This the most generic method to solve this particular problem. In this we just use the inbuilt function extended by Python library to solve this task. It checks for any element satisfying a condition and returns a True in case it finds any one element.
Python3
test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]
print ( "The original list : " + str (test_list))
res = any (ele > 10 for ele in test_list)
print ( "Does any element satisfy specified condition ? : " + str (res))
|
Output
The original list : [4, 5, 8, 9, 10, 17]
Does any element satisfy specified condition ? : True
Time Complexity: O(n) where n is the number of elements in the list “test_list”. any operator performs n number of operations.
Auxiliary Space: O(1), constant extra space is required
Method #3 : Using the next() function
Python3
test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]
print ( "The original list : " + str (test_list))
res = next ((ele for ele in test_list if ele > 10 ), False )
print ( "Does any element satisfy specified condition ? : " + str (res))
|
Output
The original list : [4, 5, 8, 9, 10, 17]
Does any element satisfy specified condition ? : 17
This code uses the next() function to check if any element in the list test_list is greater than 10. The generator expression (ele for ele in test_list if ele > 10) returns an iterator containing the first element that satisfies the condition, or False if no such element exists. The next() function is used to retrieve the first element from the generator expression. If an element greater than 10 exists in the list, next() will return that element, otherwise it will return False. In this way, we can check if any element in the list is greater than 10 by checking if the output of next() is False.
The time complexity of this method is O(n) as it iterates through the list once. The Auxiliary space of this method is O(1) as it only uses a single variable res to store the result of the check.
Method #4 : Using filter()
- We start by initializing a list, test_list, containing some elements.
- We then use the filter() function to create a new list containing only elements that are greater than 10.
- If the length of this new list is greater than 0, we set the res variable to True.
Otherwise, we leave res as False.
- We print the result.
Python3
test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]
print ( "The original list : " + str (test_list))
res = bool ( list ( filter ( lambda x: x > 10 , test_list)))
print ( "Does any element satisfy specified condition ? : " + str (res))
|
Output
The original list : [4, 5, 8, 9, 10, 17]
Does any element satisfy specified condition ? : True
Time Complexity: O(n), as it iterates through the list once.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”
Please Login to comment...