Python Program to Test if any set element exists in List


In this article, we will learn how to check if any set element exists in the list in python.

Methods Used

  • Using any() function

  • Using Bitwise & operator

  • Using Counter(), filter() and lambda functions

Example

Assume we have taken an input set and input list. We will now check whether any input set element exists in the input list using the above methods.

Input

inputSet = {4, 8, 1, 3, 5, 7}
inputList = [7, 15, 20]

Output

Checking whether any set element present in the input list: True

In the above example, 7 exists in the set as well as a list so the result is True

Method 1: Using any() function

The any() function returns True if any of the items in an iterable are true, else returns False.

Syntax

any(iterable)

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task –.

  • Create a variable to store the input set and print the given set.

  • Create another variable to store the input list.

  • Use any() function to check whether any set element present in the input list by traversing through the input set and checking whether the current element is present in the input list.

  • Print the result in boolean.

Example

The following program checks whether any input set element is present in the input list using any() function and returns True if exists else it returns False –

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20]

# checking whether any set element is present in the input list using any() function
result = any(i in inputSet for i in inputList)

# printing the output
print("Checking whether any set element present in the input list:", result)

Output

On execution, the above program will generate the following output –

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

Method 2: Using Bitwise & Operator

Bitwise & Operator − The “&” is a Bitwise operator that compares numbers(binary). It sets each bit to 1 if both bits are 1.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Convert the given input to the set using the set() function.

  • Use the & operator(sets each bit to 1 if both bits are 1) to check whether any set element is present in the input list and convert the result into a boolean using the bool() function(returns the boolean value of a given object)

  • Print the result.

Example

The following program checks whether any input set element is present in the input list using the bitwise & operator and returns True if exists else returns False –

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [9, 15, 20]

# Convert the given list to set using the set() function
inputListSet = set(inputList)

# checking whether any set element present in the input list

# using & operator(checks for common element) and converting to boolean
result = bool(inputSet & inputListSet)

# printing the output
print("Checking whether any set element present in the input list:", result)

Output

On execution, the above program will generate the following output –

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: False

Method 3: Using Counter(), filter() and lambda functions

filter() function − filters the specified sequence using a function that determines if each element in the sequence is true or false.

Counter() function − a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.

lambda() function

A lambda function is a small anonymous function.

A lambda function can have an unlimited/any number of arguments but only one expression.

Syntax

lambda arguments : expression

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword to import the Counter function from the collections module.

  • Use the Counter() function to get the frequencies of all the input list elements as a dictionary.

  • Filter all the input set elements if they are present in the above frequencies dictionary using the filter function.

  • If there are any elements in common then the filtered list will have a length greater than 1.

  • Check if the above condition using the if conditional statement and print accordingly.

Example

The following program checks whether any input set element is present in the input list using Counter(), filter(), and lambda functions and returns True if exists else returns False –

# importing a Counter function from the collections module
from collections import Counter

# input set
inputSet = {4, 8, 1, 3, 5, 7}

# printing the input set
print("Input set:\n", inputSet)

# input list
inputList = [7, 15, 20, 7]

# getting the frequency of list elements using the Counter() function

# Here it returns frequencies as a dictionary
elements_freq = Counter(inputList)

# Traversing in the input Set using the lambda function

# Checking if the set element exists in the keys of the dictionary

# Filtering all the elements which satisfy the above condition
output = list(filter(lambda k: k in elements_freq.keys(), inputSet))

# Check if there are any filtered elements
if(len(output) > 0):
   output = True

# If no elements are common then the output will be False
else:
   output = False

# printing the output
print("Checking whether any set element present in the input list:", output)

Output

On execution, the above program will generate the following output –

Input set:
{1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

Conclusion

In this article, we learned how to use three different methods to determine whether the set contains an element from the list. We also learned how to use the set() function to convert any iterable, such as a list, tuple, or any iterable, into a set, and how to use the & operator to find elements that are common by the two sets that are given.

Updated on: 27-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements