Python set intersection_update() method
Example
Remove items that do not exist in both set x and set y:
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.intersection_update(y) print(x)
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 returned 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)