set.intersection()

set.intersection(*others)
Returns: set · Updated March 15, 2026 · Set Methods
sets methods immutable-operation

The .intersection() method returns a new set containing only the elements that are present in both the calling set and all the provided iterables. This is the mathematical intersection operation, useful for finding common elements between collections.

Syntax

set.intersection(*others)

Parameters

ParameterTypeDefaultDescription
*othersiterableOne or more iterables to intersect with. Only elements present in all sets will be in the result.

Examples

Basic usage

Find common elements between two sets:

fruits_a = {"apple", "banana", "cherry"}
fruits_b = {"banana", "cherry", "date"}

common = fruits_a.intersection(fruits_b)
print(common)
# {'banana', 'cherry'}

Multiple iterables

You can intersect more than two sets at once:

a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}

result = a.intersection(b, c)
print(result)
# {3, 4}

Using with different types

The method works with any iterable, not just sets:

numbers = {1, 2, 3, 4, 5}
exclude_list = [3, 4, 6]

result = numbers.intersection(exclude_list)
print(result)
# {3, 4}

Finding overlapping items

A practical use case is finding overlapping tags or categories:

user_interests = {"python", "javascript", "rust", "go"}
job_requirements = {"python", "javascript", "java"}
matching = user_interests.intersection(job_requirements)

print(f"Matching skills: {matching}")
# Matching skills: {'python', 'javascript'}

Working with strings

Strings are iterable, so you can find common characters:

word1 = "hello"
word2 = "world"

common_chars = set(word1).intersection(set(word2))
print(common_chars)
# {'l', 'o'}

This is useful for simple anagram checking or character analysis.

intersection() vs intersection_update()

Understanding when to use which:

original = {1, 2, 3, 4}
others = {2, 3, 5}

# .intersection() returns a NEW set, original unchanged
result = original.intersection(others)
print(original)  # {1, 2, 3, 4} — unchanged
print(result)    # {2, 3} — new set

# .intersection_update() modifies in place (not shown, but similar logic)

Use .intersection() when you need to keep the original data intact. Use .intersection_update() when you want to modify the original set directly.

Common Patterns

Tag matching

post_tags = {"python", "tutorial", "beginner", "guide"}
required_tags = {"python", "tutorial"}
matching = post_tags.intersection(required_tags)

if matching == required_tags:
    print("Post has all required tags")
else:
    missing = required_tags.difference(matching)
    print(f"Missing tags: {missing}")
# Post has all required tags

Permission checking

user_permissions = {"read", "write", "delete"}
required_permissions = {"read", "write", "execute"}
granted = user_permissions.intersection(required_permissions)

if granted == required_permissions:
    print("All required permissions granted")
else:
    denied = required_permissions.difference(granted)
    print(f"Denied: {denied}")
# Denied: {'execute'}

Data filtering

Filter items that appear in multiple datasets:

jan_sales = {"alice", "bob", "charlie", "david"}
feb_sales = {"alice", "bob", "eve", "frank"}
mar_sales = {"alice", "charlie", "eve", "george"}

top_customers = jan_sales.intersection(feb_sales, mar_sales)
print(f"Customers who bought every month: {top_customers}")
# Customers who bought every month: {'alice'}

Performance

The .intersection() method has:

  • Time complexity: O(min(len(s), len(other1), len(other2), …))
  • Space complexity: O(min(len(s), len(other1), …)) for the result set

This makes it very efficient because it iterates over the smallest set and checks membership in the others.

See Also