Skip to main content

Python Set intersection() Function

The Set intersection() method finds the common elements between two or more sets. It returns a new set that contains only the items that exist in all the sets being compared.

note

It does not alter the original sets!

Syntax

my_set.intersection(set1, set2, ...)

intersection() Parameters

Python Set intersection() function parameters:

ParameterConditionDescription
set1, set2, ...OptionalA comma-separated list of one or more sets to find common items in
note

You can also pass iterables like list, tuple, dictionary or string. In that case, intersection() method first converts the iterables into sets and then computes the intersection.

intersection() Return Value

Python Set intersection() function returns a new set containing the common elements of all the sets being compared.

Examples

Example 1: Common Elements of Two Sets with intersection() method

A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}

result = A.intersection(B)

print(result) # Output: {'David', 'Tom'}

output

{'David', 'Tom'}

Example 2: Intersection between Multiple Sets

The intersection() method allows to compute the common elements between multiple sets:

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

result = A.intersection(B, C)

print(result) # Output: {6, 7}

output

{6, 7}

Example 3: Intersection between a Set and a List

You can also pass iterables like list: intersection() first converts the iterables to sets and then computes the intersection.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a List B
B = ['d', 'e', 'f']

print(A.intersection(B)) # Output: {'e'}

output

{'e'}

Example 4: Intersection between a Set and a Tuple

You can also pass iterables like tuple: intersection() first converts the iterables to sets and then computes the intersection.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a Tuple B
B = ('d', 'e', 'f')

print(A.intersection(B)) # Output: {'e'}

output

{'e'}

Example 5: Intersection between a Set and a Dictionary

You can also pass iterables like dictionary: intersection() first converts the iterables to sets and then computes the intersection.

note

The dictionary keys are used for intersection(), not the values!

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create Dictionary B and C
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}

print(A.intersection(B)) # Output: set()
print(A.intersection(C)) # Output: {'e'}

output

set()
{'e'}

Example 6: Intersection between a Set and a String

You can also pass iterables like string: intersection() first converts the iterables to sets and then computes the intersection.

For example:

# create a Set A
A = {'a', 'e', 'i', 'o', 'u'}

# create a String B
B = 'def'

print(A.intersection(B)) # Output: {'e'}

output

{'e'}

Equivalent Operator & for intersection()

Set intersection can be performed with the & operator as well, providing an alternative to the intersection() method.

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}

# by intersection() method
print(A.intersection(B)) # Output: {3, 4, 5}

# by & operator
print(A & B) # Output: {3, 4, 5}

output

{3, 4, 5}
{3, 4, 5}

Of course, you can use the & operator with multiple sets:

A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}

# by intersection() method
print(A.intersection(B, C)) # Output: {6, 7}

# by & operator
print(A & B & C) # Output: {6, 7}

output

{6, 7}
{6, 7}