The difference_update() function in python helps in set update. It first finds out the set difference between the given two sets. And update the first set with result elements of set difference. The set difference gives a new set that contains the elements that exist only in the first set, not in the second set.
A.difference_update(B) #where A & B are sets
The difference() function takes set as its parameters. After this method call set A will be updated as A-B. And set B remains unchanged.
Parameter | Description | Required / Optional |
---|---|---|
set | The set to check for differences in | Required |
This method doesn't return any value. It updates the original set with a set of different values.
A = {1, 2, 3, 4, 6}
B = {5, 2, 4, 7}
# Before update
print('A = ', A)
result = A.difference_update(B)
# After update
print('A = ', A)
print('B = ', B)
print('result = ', result)
Output:
# Before update A = {1, 2, 3, 4, 6} # After update A = {1, 3, 6} B = {5, 2, 4, 7} result = None
A = {'a', 'b', 'c', 'd'}
B = {'c', 'f', 'd'}
# Before update
print('A = ', A)
result = A.difference_update(B)
# After update
print('A = ', A)
print('B = ', B)
print('result = ', result)
Output:
# Before update A = {'a', 'b', 'c', 'd'} # After update A = {'a', 'b'} B = {'c', 'd', 'f'} result = None