Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Check if any element in list satisfies a condition

Python | Check if any element in list satisfies a condition

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




# Python3 code to demonstrate working of
# Check if any element in list satisfies a condition
# Using list comprehension
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Check if any element in list satisfies a condition
# Using list comprehension
res = True in (ele > 10 for ele in test_list)
 
# Printing result
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




# Python3 code to demonstrate working of
# Check if any element in list satisfies a condition
# Using any()
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Check if any element in list satisfies a condition
# Using any()
res = any(ele > 10 for ele in test_list)
 
# Printing result
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




# This code demonstrates how to check if any element in a list satisfies a specific condition using the next() function
 
# Initializing the list
test_list = [4, 5, 8, 9, 10, 17]
 
# Printing the original list
print("The original list : " + str(test_list))
 
# Using the next() function to check if any element in the list is greater than 10
res = next((ele for ele in test_list if ele > 10), False)
 
# Printing the result of the check
print("Does any element satisfy specified condition ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
# Output:
# The original list : [4, 5, 8, 9, 10, 17]
# Does any element satisfy specified condition ? : True


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

  1. We start by initializing a list, test_list, containing some elements.
  2. We then use the filter() function to create a new list containing only elements that are greater than 10.
  3. If the length of this new list is greater than 0, we set the res variable to True.
    Otherwise, we leave res as False.
  4. We print the result.

Python3




# Python3 code to demonstrate working of
# Check if any element in list satisfies a condition
# Using filter()
 
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
 
# printing list
print("The original list : " + str(test_list))
 
# Check if any element in list satisfies a condition
# Using filter()
res = bool(list(filter(lambda x: x > 10, test_list)))
 
# Printing result
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”

RELATED ARTICLES

Most Popular

Recent Comments