Open In App

Find Common elements in three Lists using Sets – Python

Last Updated : 19 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given three lists we need to find common elements in all three lists using sets. For example a = [1, 2, 3, 4], b = [2, 3, 5, 6] and c = [1, 2, 3, 7]. We need to find all common elements so that resultant output should be {2, 3}.

Using set.intersection()

set.intersection() method finds common elements between multiple sets by returning a new set containing only elements present in all sets. It efficiently identifies shared elements across any number of sets.

Python
a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [1, 2, 3, 7]

# Convert lists to sets and find common elements
res = set(a).intersection(b, c)
print(f"Common elements: {res}")  

Output
Common elements: {2, 3}

Explanation:

  • set(a).intersection(b, c) method converts the lists a, b, and c into sets and finds common elements shared among all three sets.
  • Resulting common elements are stored in the res set and printed as output.

Using & Operator

& operator performs a set intersection in Python returning a set containing only elements that are common to all sets involved. It is a shorthand for intersection() method providing same result.

Python
a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [1, 2, 3, 7]

# Convert lists to sets and use the & operator
res = set(a) & set(b) & set(c)
print(f"Common elements: {res}") 

Output
Common elements: {2, 3}

Explanation:

  • & operator is used to find intersection of the sets created from lists a, b, and c, returning only elements common to all sets.
  • Common elements are stored in res set and printed as result

Using Set Comprehension

Set comprehension allows the creation of a set by iterating over a sequence and applying a condition or transformation. It provides a concise way to build a set while filtering or modifying elements during iteration.

Python
a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [1, 2, 3, 7]

# Set comprehension to find common elements
res = {x for x in a if x in b and x in c}
print(f"Common elements: {res}") 

Output
Common elements: {2, 3}

Explanation:

  • Set comprehension {x for x in a if x in b and x in c} iterates over elements in a and adds them to the result if they are also present in both b and c.
  • Resulting common elements are stored in “res” set and printed as output.

Using filter() with lambda

filter() function with a lambda allows filtering elements from a sequence based on a condition. It returns an iterator with elements that satisfy condition specified by lambda function.

Python
a = [1, 2, 3, 4]
b = [2, 3, 5, 6]
c = [1, 2, 3, 7]

# Filter common elements
res = set(filter(lambda x: x in b and x in c, a))
print(f"Common elements: {res}") 

Output
Common elements: {2, 3}

Explanation:

  • filter() function with lambda expression iterates over elements in a keeping only those that are also present in both b and c.
  • Resulting common elements are converted to a set and printed as the output.


Similar Reads