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 a list using three different Python methods.

Methods Used

  • Using any() function

  • Using bitwise & operator

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

Example Problem

We have an input set and input list. We need to check whether any element from the set exists in the list ?

Input

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

Expected Output

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

In the above example, 7 exists in both the set and list, so the result is True.

Method 1: Using any() Function

The any() function returns True if any item in an iterable is true, otherwise returns False.

Syntax

any(iterable)

Example

The following program checks whether any set element exists in the list using any() function ?

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

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

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

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

# printing the output
print("Checking whether any set element present in the input list:", result)
Input set: {1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

Method 2: Using Bitwise & Operator

The bitwise & operator finds common elements between two sets. When applied to sets, it returns the intersection of both sets.

Example

The following program uses the bitwise & operator to find common elements ?

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

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

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

# Convert the list to set
inputListSet = set(inputList)

# checking whether any set element present in the input list
# using & operator to find intersection and converting to boolean
result = bool(inputSet & inputListSet)

# printing the output
print("Checking whether any set element present in the input list:", result)
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

The Counter() function counts hashable objects and creates a frequency dictionary. The filter() function filters elements based on a condition using lambda.

Example

The following program uses Counter, filter, and lambda to check for common elements ?

# importing Counter function from collections module
from collections import Counter

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

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

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

# getting frequency of list elements using Counter() function
elements_freq = Counter(inputList)

# filtering set elements that exist in the list
output = list(filter(lambda k: k in elements_freq.keys(), inputSet))

# check if there are any common elements
if len(output) > 0:
    result = True
else:
    result = False

# printing the output
print("Checking whether any set element present in the input list:", result)
Input set: {1, 3, 4, 5, 7, 8}
Checking whether any set element present in the input list: True

Comparison

Method Time Complexity Best For
any() function O(n) Simple and readable
Bitwise & operator O(min(m,n)) Set operations
Counter + filter O(n+m) When frequency matters

Conclusion

Use any() for simple readable code, bitwise & operator for efficient set operations, or Counter with filter when you need frequency information. The bitwise approach is generally most efficient for large datasets.

Updated on: 2026-03-27T00:04:28+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements