The Python set difference update() is a built-in function that allows you to update a set by removing items from the specified set (passed as arguments in the function).
To use the difference_update() method, we follow the syntax:
P.difference_update(Q)
Here, P and Q are two sets. If we perform (P-Q) using the difference_update() method, only set P gets modified to the difference value (P-Q). Set Q does not change.
If we use Q.difference_update(P), set Q is updated to (Q-P), and set P remains unchanged.
Return Value from difference_update()
Unlike the set difference() method, Python set difference_update() does not return any value (None). It indicates that the set has been mutated/changed.
In the code:
result = X.difference_update(Y)
The result will be: None, X will be equal to (X-Y), and Y will remain unchanged.
Example of how difference_update() works
Let us now look at an example to understand how the Python set difference_update() method works in a program.
# creating two sets
num1 = { 45, 23, 55, 11, 46, 33, 94, 29, 40}
num2 = { 11, 45, 38, 82, 33, 40, 55, 73, 98}
# using difference_update()
result = num1.difference_update(num2)
print ('First row =', num1)
print ('Second row =', num2)
print ('Result =', result)
Output:
First row = {46, 23, 29, 94}
Second row = {33, 98, 38, 40, 73, 11, 45, 82, 55}
Result = None
Questions and Answers
Q1. What does the Python set difference_update() method do?
The set difference_update() is a built-in function of Python that allows you to remove elements of a set that are members of the specified set arguments. The set that calls the method is modified to the difference, and the set argument remains unchanged.
Q2. How does set difference_update() work in Python?
To use the difference_update() with Python sets, we follow the syntax:
P.difference_update(Q)
Where P and Q are two sets.
Example:
# set
hob1 = {'swimming', 'skating', 'singing', 'dancing'}
hob2 = {'swimming', 'singing'}
hob1.difference_update(hob2)
print ('Previous Hobbies:', hob1)
print ('Current Hobbies:', hob2)
Output:
Previous Hobbies: {'skating', 'dancing'}
Current Hobbies: {'swimming', 'singing'}
Q3. Is the Python set difference_update() different from difference()?
Yes. The set difference() method finds the difference between two sets and returns a new set with the value. The Python set difference_update() method modifies/updates the existing caller set.
Example:
# sets
num1 = { 45, 23, 55, 11, 46, 33, 94, 29, 40}
num2 = { 11, 45, 38, 82, 33, 40, 55, 73, 98}
# difference() method
num = num1.difference(num2)
# difference_update()
result = num1.difference_update(num2)
print ('With difference():', num)
print ('With difference_update(): num1=', num1)
print ('With difference_update(): num2=', num2)
Output:
With difference(): {94, 29, 46, 23}
With difference_update(): num1= {46, 23, 29, 94}
With difference_update(): num2= {33, 98, 38, 40, 73, 11, 45, 82, 55}
Q4. Can I use difference_update() with multiple sets?
Yes. You can use the difference_update method() with multiple sets by separating them with commas.
# create three sets
A = {'rose', 'lily', 'marigold', 'cactus', ' orchid', 'lotus'}
B = {'lily', 'rafflesia', 'tulip', 'rose', 'carnation', 'hyacinth'}
C = {'freesia', 'chrysanthemum', 'daffodil', 'rose', 'tulip'}
# using difference_update()
result = A.difference_update(B,C)
print ('A =', A)
print ('B =', B)
print ('C =', C)
print ('Result =', result)
Output:
A = {'cactus', 'marigold', ' orchid', 'lotus'}
B = {'lily', 'tulip', 'hyacinth', 'rafflesia', 'carnation', 'rose'}
C = {'tulip', 'freesia', 'chrysanthemum', 'daffodil', 'rose'}
Result = None
Leave a Reply