Open In App

Python | Check if any element in list satisfies a condition

Last Updated : 01 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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”



Previous Article
Next Article

Similar Reads

Python | Test if any list element returns true for condition
Sometimes, while coding in Python, we can have a problem in which we need to filter a list on basis of condition met by any of the element. This can have it's application in web development domain. Let's discuss a shorthand in which this task can be performed. Method : Using any() + list comprehension The simplest way and shorthand to solve this pr
4 min read
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 all() We can use all(), to perform this particular
5 min read
Python | Check if any element occurs n times in given list
Given a list, the task is to find whether any element occurs 'n' times in given list of integers. It will basically check for the first element that occurs n number of times. Examples: Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3 Output : 2 Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4 Output : 1 Below are some methods to do the task i
5 min read
Python - Check if string starts with any element in list
While working with strings, their prefixes and suffix play an important role in making any decision. Let’s discuss certain ways in which this task can be performed. Example: String = "GfG is best" Input_lis = ['best', 'GfG', 'good'] Output: True Explanation: 'GfG is best' is present in the list. String = "GfG is best" Input_lis = ['Good', 'Bad', 'N
4 min read
Python - Check if any list element is present in Tuple
Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements. Method #1: Using loop In this, we keep a boolean v
6 min read
Python program to find all the Combinations in the list with the given condition
Given a list with some elements being a list of optional elements. The task is to find all the possible combinations from all options. Examples: Input: test_list = [1,2,3] Output: [1], [1, 2], [1, 2, 3], [1, 3] [2], [2, 3], [3] Example 1: Get all possible combinations of a list’s elements using combinations C/C++ Code from itertools import combinat
3 min read
Remove Elements From a List Based on Condition in Python
In Python, lists are a versatile and widely used data structure. There are often situations where you need to remove elements from a list based on a specific condition. In this article, we will explore five simple and commonly used methods to achieve this task. Remove Elements From A List Based On A Condition In PythonBelow, are the methods of Remo
3 min read
Python | Check if two lists have any element in common
Sometimes we encounter the problem of checking if one list contains any element of another list. This kind of problem is quite popular in competitive programming. Let's discuss various ways to achieve this particular task. Method #1: Using any() C/C++ Code # Python code to check if two lists # have any element in common # Initialization of list lis
5 min read
Python - Test if any set element exists in List
Given a set and list, the task is to write a python program to check if any set element exists in the list. Examples: Input : test_dict1 = test_set = {6, 4, 2, 7, 9, 1}, test_list = [6, 8, 10] Output : True Explanation : 6 occurs in list from set. Input : test_dict1 = test_set = {16, 4, 2, 7, 9, 1}, test_list = [6, 8, 10] Output : False Explanation
4 min read
Check if elements of an array can be arranged satisfying the given condition
Given an array arr of N (even) integer elements. The task is to check if it is possible to reorder the elements of the array such that: arr[2*i + 1] = 2 * A[2 * i] for i = 0 ... N-1. Print True if it is possible, otherwise print False. Examples: Input: arr[] = {4, -2, 2, -4} Output: True {-2, -4, 2, 4} is a valid arrangement, -2 * 2 = -4 and 2 * 2
5 min read
Practice Tags :