Python verzameling intersection_update() methode

Example

Verwijder items die niet in de verzameling x en verzameling y voorkomen:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y) 
print(x)

Run Instance

Definition and Usage

The intersection_update() method will remove items that do not exist in all sets.

The intersection_update() method is different from the intersection() method because the intersection() method returns a new set without unnecessary items, while the intersection_update() method removes unnecessary items from the original set.

Syntax

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

Parameter Value

Parameter Description
set1 Required. The set in which to retrieve equal items.
set2

Optional. The other set in which to retrieve equal items.

You can compare any number of sets.

Sets are separated by commas.

More Examples

Example

Compare 3 sets, the resulting set contains items that exist in all 3 sets:

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
x.intersection_update(y, z)
print(x)

Run Instance