Introduction: Python Set intersection() Method
In Python, a common requirement is to compare multiple datasets and find values that are shared between them. Lists can make this process longer because you need extra logic to check matches manually.
This is where the Python set intersection() method becomes useful for handling such comparisons efficiently.
What it is: The intersection() method is a built-in Python set method that returns a new set containing only the elements common to all given sets.
It does not modify the original set, which makes it safe to use when the original data needs to remain unchanged.
Take a look at a quick example to see how it works.
You can also explore its real-world use cases.
Understanding the syntax and parameters first will make it easier to follow the examples and use cases later.
Tip: Learn how intersection compares with other operations in the Python Sets operations guide.
Syntax, Parameters, Return Value and Examples: Python Set intersection() Method
Take a quick look at the syntax and parameters below to understand the working flow of this method.
Syntax
new_set = set1.intersection(set2, set3, ...)
Parameters
| Parameter | Description |
|---|---|
| set2, set3, … | One or more sets whose common elements with set1 will be returned. |
Returns
| Return Value | Description |
|---|---|
| set | Returns a new set containing only the elements common to all specified sets. |
Quick Example
A simple case where common elements between two sets are extracted.
a = {1, 2, 3}
b = {2, 3, 4}
result = a.intersection(b)
print(result)
# Output:
# {2, 3}
The method compares both sets and returns only the values that exist in both.
How the Python set intersection() method works
- The intersection() method compares multiple sets.
- Only shared values are selected.
- Non-matching values are ignored.
- The original set is not modified.
- A new set is always returned.
Practical Examples: Set intersection() Method
Below are simple to advanced examples of Python set intersection() method showing how it behaves in different situations:
Simple Level Examples
Example 1: Basic Intersection
a = {"apple", "banana", "cherry"}
b = {"banana", "mango", "cherry"}
print(a.intersection(b))
# Output:
# {'banana', 'cherry'}
Explanation: Only elements present in both sets are returned.
Example 2: No Common Elements
x = {"red", "green"}
y = {"blue", "yellow"}
print(x.intersection(y))
# Output:
# set()
Explanation: Since there are no common values, the result is an empty set.
Example 3: Single Common Value
nums1 = {1, 2, 3}
nums2 = {3, 4, 5}
print(nums1.intersection(nums2))
# Output:
# {3}
Explanation: Only the value 3 exists in both sets.
Example 4: Identical Sets
lang1 = {"Python", "Java"}
lang2 = {"Python", "Java"}
print(lang1.intersection(lang2))
# Output:
# {'Python', 'Java'}
Explanation: Since both sets are identical, all elements are returned.
Example 5: Case Sensitivity
set1 = {"Python", "Java"}
set2 = {"python", "JAVA"}
print(set1.intersection(set2))
# Output:
# set()
Explanation: Different letter cases are treated as different values, so no match is found.
Medium Level Examples
Example 6: Multiple Set Comparison
set1 = {1, 2, 3, 4}
set2 = {2, 3, 5}
set3 = {2, 3, 6}
print(set1.intersection(set2, set3))
# Output:
# {2, 3}
Explanation: Only values that exist in every set are returned.
Example 7: Intersection in Condition
a = {"dog", "cat"}
b = {"rabbit", "dog"}
if a.intersection(b):
print("Overlap found")
else:
print("No common items")
# Output:
# Overlap found
Explanation: Since there is at least one common value, the condition becomes True.
High Level Examples
Example 8: Common Users Across Groups
group_A = {"Alice", "Bob", "Charlie"}
group_B = {"Charlie", "David"}
group_C = {"Charlie", "Eve"}
common = group_A.intersection(group_B, group_C)
print(common)
# Output:
# {'Charlie'}
Explanation: Only the user present in all groups is returned.
Example 9: Common Tags Across Data
tags1 = {"python", "ai", "ml"}
tags2 = {"ml", "data", "python"}
tags3 = {"python", "ml", "cloud"}
common_tags = tags1.intersection(tags2, tags3)
print(common_tags)
# Output:
# {'python', 'ml'}
Explanation: The method keeps only tags that are common across all datasets.
Use Cases: When to use the set intersection() method
Below are some common use cases of the Python set intersection() method:
- Comparing user selections or preferences
- Finding shared tags, categories, or labels
- Matching skills, permissions, or roles
- Ensuring consistency across datasets
- Filtering overlapping values from multiple inputs
Key Takeaways: Set intersection() Method
Here are the core ideas you should keep in mind when using Python set intersection() method:
- intersection() returns only common elements across sets.
- It supports multiple sets for comparison.
- The original set remains unchanged.
- It always returns a new set.
- Returns an empty set if no elements match.
In short, Python set intersection() method helps you quickly find shared values across datasets without modifying the original data.