Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Test if any set element exists in List

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 : No set element exists in list.

Method #1 : Using any()

In this, we iterate for all the elements of the set and check if any occurs in the list. The any(), returns true for any element matching condition.

Python3




# Python3 code to demonstrate working of
# Test if any set element exists in List
# Using any()
 
# initializing set
test_set = {6, 4, 2, 7, 9, 1}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing list
test_list = [6, 8, 10]
 
# any() checking for any set element in check list
res = any(ele in test_set for ele in test_list)
 
# printing result
print("Any set element is in list ? : " + str(res))


Output:

The original set is : {1, 2, 4, 6, 7, 9}
Any set element is in list ? : True

Time complexity: O(n) where n is the length of test_list. 
Auxiliary space: O(1). The memory used by the program is constant and does not increase with the size of the input.

Method #2 : Using & operator

In this, we check for any element by using and operation between set and list, if any element matches, the result is True.

Python3




# Python3 code to demonstrate working of
# Test if any set element exists in List
# Using & operator
 
# initializing set
test_set = {6, 4, 2, 7, 9, 1}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing list
test_list = [6, 8, 10]
 
# & operator checks for any common element
res = bool(test_set & set(test_list))
 
# printing result
print("Any set element is in list ? : " + str(res))


Output:

The original set is : {1, 2, 4, 6, 7, 9}
Any set element is in list ? : True

Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(m), where m is the length of the test_set converted to a set.

Method #3:Using Counter()+lambda functions

Python3




# Python3 code to demonstrate working of
# Test if any set element exists in List
from collections import Counter
 
# initializing set
test_set = {6, 4, 2, 7, 9, 1}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing list
test_list = [6, 8, 10]
 
freq = Counter(test_list)
res = len(list(filter(lambda x: x in freq.keys(), test_set))) > 0
 
# printing result
print("Any set element is in list ? : " + str(res))


Output

The original set is : {1, 2, 4, 6, 7, 9}
Any set element is in list ? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method#4: using set intersection

Algorithm:

  1. Initialize the test_set and test_list.
  2. Use set intersection() method to check if there is any element present in both sets.
  3. The intersection() method returns a set of common elements between the two sets.
  4. Check if the returned set is empty or not using the bool() function.
  5. Print the result.

Python3




# Initializing set
test_set = {6, 4, 2, 7, 9, 1}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing list
test_list = [6, 8, 10]
 
# checking for any set element in check list using set intersection
res = bool(test_set.intersection(test_list))
 
# printing result
print("Any set element is in list ? : " + str(res))


Output

The original set is : {1, 2, 4, 6, 7, 9}
Any set element is in list ? : True

Time Complexity:
The time complexity of the set intersection() method is O(min(len(test_set), len(test_list))), as it iterates over the smaller set to check the common elements. So the overall time complexity of the code is O(min(len(test_set), len(test_list))).

Auxiliary Space:
The set intersection() method creates a set of common elements, which takes extra space. However, the space required by this set is proportional to the number of common elements between the two sets. So the auxiliary space complexity of the code is O(min(len(test_set), len(test_list))).

RELATED ARTICLES

Most Popular

Recent Comments