Python set difference_update() Method

A.difference_update(*others) removes from A every element found in the other iterables and returns None. It changes the original set in place because sets are mutable.

If you need a new result while keeping A unchanged, use difference() or the - operator.

Set order: sets are unordered. Whenever an example prints a set, the displayed element order can differ between runs or Python environments.
set operation difference

Using difference_update() method

A={'a','b','c'}
B={'a','y','z'}
A.difference_update(B)
print(A)
Output
{'c','b'}

Using More Than Two Sets

We can use any number of sets with difference_update()
A={'a','b','c'}
B={'a','y','z'}
C={'a','c','l'}
A.difference_update(B,C)
print(A)
Output
{'b'}

difference_update() method

Using string ( iterable object ) with difference_update() method
A={'a','b','c','x','y'}
B='Alex'
A.difference_update(B)
print(A)
Output
{'c', 'y', 'a', 'b'}
Using list with difference_update() method.
A={'a','b','c'}
B=['a','x','y']
A.difference_update(B)
print(A)
Output
{'c', 'b'}
All Set Methods Updating Set Operations Questions with solutions on set
difference() union() intersection() symmetric_difference()


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer