Python Set intersection_update() Method

Python3 列表Python Sets


Description

intersection_update()This method is used to obtain the elements present in two or more sets, i.e., compute the intersection.

intersection_update()method differs fromintersection()method, becauseintersection()method returns a new set, whileintersection_update()method removes non-overlapping elements from the original set.

Syntax

intersection_update() method syntax:

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

Parameters

  • set1 -- Required, the set to search for the same elements
  • set2 -- Optional, other sets to search for the same elements, can be multiple; multiple sets are separated by commas

Return Value

None.

Examples

Remove elements from set x that do not exist in set y:

Example 1

x = {"apple", "banana", "cherry"} # y set does not contain banana and cherry, so they are removed y = {"google", "example", "apple"} x.intersection_update(y) print(x)

The output result 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 result is:

{'c'}

Python3 列表Python Sets

Other Extensions