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 problem is to combine the functionalities of inbuilt any() and list comprehension for rendering condition logic and list iteration. The any() returns true if any of the list element matches the condition.
Python3
# Python3 code to demonstrate working of # Test if any list element returns true for condition # Using any() + list comprehension # initializing list test_list = [ 6 , 4 , 8 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # Test if any list element returns true for condition # Using any() + list comprehension res = any (x % 5 for x in test_list) # Printing result print ( "Does list contain any element divisible by 5? : " + str (res)) |
The original list : [6, 4, 8, 9, 10] Does list contain any element divisible by 5? : True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 2 : Using lambda functions
Python3
# Python3 code to demonstrate working of # Test if any list element returns true for condition # initializing list test_list = [ 6 , 4 , 8 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # Test if any list element returns true for condition res = list ( filter ( lambda x: x % 5 = = 0 , test_list)) if (res): res = True else : res = False # Printing result print ( "Does list contain any element divisible by 5? : " + str (res)) |
The original list : [6, 4, 8, 9, 10] Does list contain any element divisible by 5? : True
Time Complexity:O(N)
Auxiliary Space: O(N)
Method 3 : Using a for loop:
Python3
test_list = [ 6 , 4 , 8 , 9 , 10 ] for i in test_list: if i % 5 = = 0 : print ( "Does list contain any element divisible by 5? : True" ) break else : print ( "Does list contain any element divisible by 5? : False" ) #This code is contributed by Jyothi pinjala. |
Does list contain any element divisible by 5? : True
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 4: Using recursion
Python3
def check(l,i): if i = = len (l): return False if l[i] % 5 = = 0 : return True return check(l,i + 1 ) test_list = [ 6 , 4 , 8 , 9 , 10 ] res = check(test_list, 0 ) print ( "Does list contain any element divisible by 5? : " ,res) #this code is contributed by Vinay Pinjala. |
Does list contain any element divisible by 5? : True
Time Complexity: O(n)
Auxiliary space: O(n)
Method 6: Using the set and intersection method
approach using the set and intersection methods:
- Initialize the list of integers.
- Convert the list into a set to remove any duplicates.
- Create a set containing only the multiples of 5.
- Compute the intersection of the two sets.
- If the resulting set is not empty, the list contains an element divisible by 5, so set the result to True, otherwise set it to False.
- Print the result.
Python3
# Python3 code to demonstrate working of # Test if any list element returns true for condition # initializing list test_list = [ 6 , 4 , 8 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # Test if any list element returns true for condition test_set = set (test_list) multiple_of_5 = set ( range ( 0 , max (test_set) + 1 , 5 )) res = bool (test_set.intersection(multiple_of_5)) # Printing result print ( "Does list contain any element divisible by 5? : " + str (res)) |
The original list : [6, 4, 8, 9, 10] Does list contain any element divisible by 5? : True
Time complexity: O(n), where n is the length of the list, but the actual time complexity depends on the size of the largest integer in the list.
Auxiliary space: O(n), where n is the length of the list, due to the creation of the test_set and multiple_of_5 sets.