Python Set difference_update() Method
Description
The difference_update() method removes elements that exist in both sets.
difference_update() The method and the difference() The difference between the methods is that difference() The method returns a new set with the common elements removed, whereas difference_update() The method removes the elements directly in the original set and has no return value.
Syntax
Syntax of the difference_update() method:
set.difference_update(set)
Parameters
- set -- required, the set used to compute the difference
Return Value
none.
Example
Remove elements contained in both sets:
Example 1
x
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
y
= {
"
"
,
"
microsoft
"
,
"
apple
"
}
x
.
difference_update
(
y
)
(
x
)
The output is:
{'cherry', 'banana'}
Python Sets