The Set difference_update() method finds the difference between two sets. It modifies the given set that contains all the elements of the first set that are not present in the second set.
Syntax
my_set.difference_update(set1, set2, ...)difference_update() Parameters
Python Set difference_update() method parameters:
| Parameter | Condition | Description |
|---|---|---|
set1, set2, ... |
Optional | A comma-separated list of one or more sets to find differences in |
difference_update() Return Value
Python Set difference_update() method does not return any value: it modifies the original set in-place.
Examples
Example 1: Difference between Two Sets
A = {'Tom', 'Anna', 'David'}
B = {'David', 'Tom'}
A.difference_update(B)
print(A) # Output: {'Anna'}output
{'Anna'}Example 2: Difference between Multiple Sets
The difference_update() method allows to compute the difference between multiple sets:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
A.difference_update(B, C)
print(A) # Output: {1, 2}output
{1, 2}Example 3: Difference between a Set and a List
You can also pass iterables like list: difference_update() first converts the iterables to sets and then computes the difference in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ['d', 'e', 'f']
A.difference_update(B)
print(A) # Output: {'i', 'a', 'o', 'u'}output
{'i', 'a', 'o', 'u'}Example 4: Difference between a Set and a Tuple
You can also pass iterables like tuple: difference_update() first converts the iterables to sets and then computes the difference in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = ('d', 'e', 'f')
A.difference_update(B)
print(A) # Output: {'i', 'a', 'o', 'u'}output
{'i', 'a', 'o', 'u'}Example 5: Difference between a Set and a Dictionary
You can also pass iterables like dictionary: difference_update() first converts the iterables to sets and then computes the difference in-place.
For example:
A1 = {'a', 'e', 'i', 'o', 'u'}
A2 = {'a', 'e', 'i', 'o', 'u'}
B = {1: 'd', 2: 'e', 3: 'f'}
C = {'d': 1, 'e': 2, 'f': 3}
A1.difference_update(B)
A2.difference_update(C)
print(A1) # Output: {'o', 'e', 'i', 'a', 'u'}
print(A2) # Output: {'o', 'a', 'u', 'i'}output
{'o', 'e', 'i', 'a', 'u'}
{'o', 'a', 'u', 'i'}Example 6: Difference between a Set and a String
You can also pass iterables like string: difference_update() first converts the iterables to sets and then computes the difference in-place.
For example:
A = {'a', 'e', 'i', 'o', 'u'}
B = 'def'
A.difference_update(B)
print(A) # Output: {'i', 'a', 'o', 'u'}output
{'i', 'a', 'o', 'u'}Equivalent Operator –= for difference_update()
Set difference can be performed with the -= operator as well, providing an alternative to the difference_update() method.
For example:
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
A -= B
print(A) # Output: {1, 2}output
{1, 2}Of course, you can use the -= operator with multiple sets:
A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
B = {3, 4, 5, 6, 7}
C = {6, 7, 8, 9, 10}
A -= B | C
print(A) # Output: {1, 2}output
{1, 2}