5 Best Ways to Find Common Elements in Two Sets Using Python

πŸ’‘ Problem Formulation: When working with sets in Python, a common task is to find elements that two sets have in common. This is akin to discovering the intersection between two groups. For example, if set A contains {1, 2, 3} and set B contains {3, 4, 5}, we seek to retrieve the common element {3}.

Method 1: Intersection Using the & Operator

The & operator between two sets returns a new set with elements common to both sets. It’s a straightforward and readable method to achieve an intersection.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

set_a = {1, 2, 3}
set_b = {3, 4, 5}
common_elements = set_a & set_b
print(common_elements)

Output: {3}

The & operator finds common items between set_a and set_b. It’s a clean and efficient one-liner that works well with small sets.

Method 2: Intersection Using the intersection() Method

The intersection() method of a set takes another set as an argument and returns a new set that contains all items that are common to both sets. It’s clear and explicitly states the operation.

Here’s an example:

set_a = {1, 2, 3}
set_b = {3, 4, 5}
common_elements = set_a.intersection(set_b)
print(common_elements)

Output: {3}

This code utilizes set_a.intersection(set_b) to compute the intersection which is {3}. This method is preferred when code readability is a priority.

Method 3: Intersection Using the intersection_update() Method

The intersection_update() method updates the set calling the method with the intersection of sets. This is useful when you want to modify the original set rather than create a new one.

Here’s an example:

set_a = {1, 2, 3}
set_b = {3, 4, 5}
set_a.intersection_update(set_b)
print(set_a)

Output: {3}

By calling set_a.intersection_update(set_b), we modify set_a in place to contain only the common elements. This saves memory when working with large sets.

Method 4: Using List Comprehension

If your sets are stored as lists, or you require the result in list form for further processing, a list comprehension can be used for finding common elements.

Here’s an example:

list_a = [1, 2, 3]
list_b = [3, 4, 5]
common_elements = [element for element in list_a if element in list_b]
print(common_elements)

Output: [3]

This approach iterates over list_a and collects elements that are also present in list_b. List comprehensions are a powerful feature in Python but might be less efficient for large lists.

Bonus One-Liner Method 5: Using a Generator Expression with next()

If you are only interested in finding the first common element, a generator expression with the next() function can be used.

Here’s an example:

set_a = {1, 2, 3}
set_b = {3, 4, 5}
common_element = next((element for element in set_a if element in set_b), None)
print(common_element)

Output: 3

This one-liner uses a generator expression to create an iterator and next() returns the first item found. If no common element exists, it defaults to None.

Summary/Discussion

  • Method 1: Intersection with & Operator. Efficient for small sets. Syntax may be unfamiliar to newcomers.
  • Method 2: Intersection Using intersection() Method. Explicit and readable. Slightly more verbose than the operator.
  • Method 3: Intersection Using intersection_update() Method. Alters original set, which can be efficient or destructive based on the use-case.
  • Method 4: Using List Comprehension. Flexible and pythonic for lists but not as efficient for large data sets.
  • Method 5: Using a Generator Expression with next(). Quick way to find a single common element, but not useful when all common elements are needed.