Of course! In Python, "intersection" most commonly refers to finding the common elements between two or more sets or lists. The most efficient and Pythonic way to do this is by using the built-in set data type.

Here’s a comprehensive guide covering different scenarios, from the most common to more advanced use cases.
The Best Way: Using set.intersection() (for Sets)
This is the most efficient and recommended method. Sets are implemented using hash tables, making their intersection operation extremely fast, with an average time complexity of O(min(len(set1), len(set2))).
Method 1: The & Operator
This is the most concise and readable way.
# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Use the & operator
intersection_set = set1 & set2
print(intersection_set)
# Output: {4, 5}
Method 2: The .intersection() Method
This method is more flexible as it can accept any iterable (like lists or tuples) as an argument, not just another set. It will automatically convert the argument to a set for the operation.

# Define a set and a list
set1 = {1, 2, 3, 4, 5}
list2 = [4, 5, 5, 6, 7, 8] # Note the duplicate 5
# Use the .intersection() method
intersection_set = set1.intersection(list2)
print(intersection_set)
# Output: {4, 5} (Duplicates in the original list are ignored)
Finding Intersection of More Than Two Sets
You can chain the operators or methods.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7}
set3 = {4, 7, 8, 9}
# Using the &
intersection_all = set1 & set2 & set3
print(f"Using &: {intersection_all}") # Output: {4}
# Using the .intersection() method
intersection_all_method = set1.intersection(set2, set3)
print(f"Using .intersection(): {intersection_all_method}") # Output: {4}
Intersection of Lists (Preserving Order and Duplicates)
If you have lists and need to find their common elements, you cannot directly use the & operator. You must first convert them to sets.
Important: This method will lose the original order of the elements and remove duplicates from the result.
list1 = [1, 2, 3, 4, 5, 5, 6] list2 = [4, 5, 5, 6, 7, 8] # Convert lists to sets and find the intersection intersection_set = set(list1).intersection(list2) # If you need the result as a list, convert it back intersection_list = list(intersection_set) print(intersection_list) # Output: [4, 5, 6] (Order is not preserved)
Advanced: Intersection of Lists Preserving Order
What if you need the intersection of two lists but also want to preserve the order from the first list? This requires a different approach.

def ordered_intersection(list1, list2):
"""
Finds the intersection of two lists, preserving the order
of the first list and removing duplicates.
"""
# Create a set from the second list for fast O(1) lookups
set2 = set(list2)
# Use a list comprehension to iterate through list1 and keep
# only elements that are also in set2.
# Using a seen set to ensure the result has no duplicates.
seen = set()
result = []
for item in list1:
if item in set2 and item not in seen:
result.append(item)
seen.add(item)
return result
list1 = [3, 1, 2, 4, 2, 5, 6]
list2 = [4, 5, 2, 7, 8]
intersection_ordered = ordered_intersection(list1, list2)
print(intersection_ordered)
# Output: [2, 4, 5, 6] (Order is from list1, no duplicates)
Intersection of Dictionaries
Finding the intersection of dictionaries can mean a few different things.
Scenario A: Intersection of Keys
This is the most common interpretation. You want to find keys that exist in both dictionaries.
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 20, 'c': 30, 'd': 40}
# Find common keys using set intersection
common_keys = dict1.keys() & dict2.keys()
print(common_keys)
# Output: {'b', 'c'}
# If you need the result as a list
common_keys_list = list(common_keys)
print(common_keys_list)
# Output: ['b', 'c'] (or ['c', 'b'], order is not guaranteed)
Scenario B: Intersection of Key-Value Pairs
You want to find key-value pairs that are identical in both dictionaries.
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 30, 'd': 40}
# Create a set of (key, value) tuples for each dictionary
items1 = set(dict1.items())
items2 = set(dict2.items())
# Find the intersection of these sets
common_items = items1 & items2
print(common_items)
# Output: {('b', 2)}
# If you need the result as a dictionary
common_dict = dict(common_items)
print(common_dict)
# Output: {'b': 2}
Summary: Which Method to Use?
| Your Goal | Recommended Method | Why? |
|---|---|---|
| Find common elements (any data type) | set(list1).intersection(list2) |
Fastest and most Pythonic. |
| Find common elements of sets | set1 & set2 or set1.intersection(set2) |
Most concise and readable for sets. |
| Find intersection of lists (order doesn't matter) | list(set(list1) & set(list2)) |
Simple and efficient. Accepts that order and duplicates are lost. |
| Find intersection of lists (order matters) | Use the ordered_intersection() function from above. |
Preserves order from the first list and removes duplicates. |
| Find common keys in dictionaries | dict1.keys() & dict2.keys() |
Clean, readable, and efficient. |
| Find common key-value pairs in dictionaries | set(dict1.items()) & set(dict2.items()) |
Directly compares the key-value pairs. |
