Skip to main content

Python Set intersection_update() Function

The Set intersection_update() method finds the common elements between two or more sets. It modifies the given set by updating it to contain only the elements that are present in both the original set and the second set (or all sets if the comparison is done between more than two sets).

Syntax

my_set.intersection_update(set1, set2, ...)

intersection_update() Parameters

Python Set intersection_update() 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_update() method first converts the iterables into sets and then computes the intersection.

intersection_update() Return Value

Python Set intersection_update() function does not return any value: it modifies the original set in-place.

Examples

Example 1: Intersection between Two Sets

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

A.intersection_update(B)

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

output

{'David', 'Tom'}

Example 2: Intersection between Multiple Sets

The intersection_update() 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}

A.intersection_update(B, C)

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

output

{6, 7}

Example 3: Intersection between a Set and a List

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

For example:

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

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

A.intersection_update(B)

print(A) # Output: {'e'}

output

{'e'}

Example 4: Intersection between a Set and a Tuple

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

For example:

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

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

A.intersection_update(B)

print(A) # Output: {'e'}

output

{'e'}

Example 5: Intersection between a Set and a Dictionary

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

note

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

For example:

# create a Set A1 and A2
A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}

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

A1.intersection_update(B)
A2.intersection_update(C)

print(A1) # Output: set()
print(A2) # Output: {'e'}

output

set()
{'e'}

Example 6: Intersection between a Set and a String

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

For example:

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

# create a String B
B = 'def'

A.intersection_update(B)

print(A) # Output: {'e'}

output

{'e'}

Equivalent Operator &= for intersection_update()

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

For example:

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

# equivalent to: A.intersection_update(B)
A &= B

print(A) # Output: {3, 4, 5}

output

{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}

# equivalent to: A.intersection_update(B, C)
A &= B & C

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

output

{6, 7}