Tutorial Study Image

Python difference_update()

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
 

difference_update() Parameters:

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

difference_update() Return Value

This method doesn't return any value. It updates the original set with a set of different values.

Examples of difference_update() method in Python

Example 1: How difference_update() works in Python?


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

Example 2: Working of difference_update() in Python?


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