Python | Test if any list element returns true for condition
Last Updated :
23 Apr, 2023
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))
OutputThe 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))
OutputThe 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.
OutputDoes 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.
OutputDoes 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))
OutputThe 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.
Similar Reads
Python - Test if string contains element from list Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Pythons = "Python is powerful an
3 min read
Check if any element in list satisfies a condition-Python 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 conditio
2 min read
Python | Test if element is dictionary value Sometimes, while working with a Python dictionary, we have a specific use case in which we just need to find if a particular value is present in the dictionary as it's any key's value. This can have use cases in any field of programming one can think of. Let's discuss certain ways in which this prob
4 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},
4 min read
Python - Test if tuple list has Single element Given a Tuple list, check if it is composed of only one element, used multiple times. Input : test_list = [(3, 3, 3), (3, 3), (3, 3, 3), (3, 3)] Output : True Explanation : All elements are equal to 3. Input : test_list = [(3, 3, 3), (3, 3), (3, 4, 3), (3, 3)] Output : False Explanation : All elemen
7 min read
Python - Test for Empty Dictionary Value List Given a dictionary with list as values, check if all lists are empty. Input : {"Gfg" : [], "Best" : []} Output : True Explanation : Both lists have no elements, hence True. Input : {"Gfg" : [], "Best" : [4]} Output : False Explanation : "Best" contains element, Hence False. Method #1 : Using any() +
6 min read