طريقة intersection_update() لمجموعات بايثون

Instance

حذف العناصر التي لا توجد في المجموعتين x و y:

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 Values

Parameters Description
set1 Required. To retrieve the set that contains equal items.
set2

Optional. To retrieve another set that contains equal items.

You can compare any number of sets.

Sets are separated by commas.

More Instances

Instance

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)

Run Instance