Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Set intersection_update() Method

Python Set intersection_update() Method

Python3 Lists Python Sets


Description

intersection_update() The method gets the elements that overlap in two or more sets, that is, it computes the intersection.

intersection_update() The method differs from intersection() method, because the intersection() The method returns a new set, whereas intersection_update() The method removes non-overlapping elements from the original set.

Syntax

Syntax of the intersection_update() method:


set.intersection_update(set1, set2 ... etc)

Parameters

Return Value

none.

Example

Remove elements from set x that are not in set y:

Example 1

x

= {

"

apple

"

,

"

banana

"

,

"

cherry

"

}

Set y does not contain banana and cherry, so they are removed

y

= {

"

google

"

,

"

runoob

"

,

"

apple

"

}

x

.

intersection_update

(

y

)

print

(

x

)

The output is:


{'apple'}

Compute the intersection of multiple sets:

Example 1

x

= {

"

a

"

,

"

b

"

,

"

c

"

}

y

= {

"

c

"

,

"

d

"

,

"

e

"

}

z

= {

"

f

"

,

"

g

"

,

"

c

"

}

x

.

intersection_update

(

y

,

z

)

print

(

x

)

The output is:


{'c'}

Python3 Lists Python Sets