Python sets come with several built-in methods to perform mathematical set operations and manage set elements efficiently. Here's a categorized breakdown with examples.
1. Add & Update Elements Method Description add(elem)
Adds an element to the set update(iterable)
Adds multiple elements from iterable
s = {1, 2}
s.add(3)
s.update([4, 5])
print(s) # Output: {1, 2, 3, 4, 5}
2. Remove Elements Method Description remove(elem)
Removes element, raises error if missing discard(elem)
Removes element, no error if missing pop()
Removes and returns a random element clear()
Removes all elements from the set
s = {1, 2, 3}
s.remove(2) # or s.discard(2)
popped = s.pop()
s.clear()
print(s) # Output: set()
3. Set Union & Joining Method Description union(other)
Returns new set with all items update(other)
Adds items from another set
a = {1, 2}
b = {2, 3}
print(a.union(b)) # Output: {1, 2, 3}
a.update(b) # a becomes {1, 2, 3}
4. Set Intersection (Common Elements) Method Description intersection(other)
Returns common elements as a new set intersection_update(other)
Keeps only common elements in the original set
a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b)) # Output: {2, 3}
a.intersection_update(b)
print(a) # Output: {2, 3}
5. Set Difference (Unique to the First Set) Method Description difference(other)
Returns items only in the first set difference_update(other)
Removes common items from the original set
a = {1, 2, 3}
b = {2, 3}
print(a.difference(b)) # Output: {1}
a.difference_update(b)
print(a) # Output: {1}
6. Symmetric Difference (Unique in Each Set) Method Description symmetric_difference(other)
Returns elements not common in both sets symmetric_difference_update(other)
Keeps only unique elements from both sets
a = {1, 2}
b = {2, 3}
print(a.symmetric_difference(b)) # Output: {1, 3}
a.symmetric_difference_update(b)
print(a) # Output: {1, 3}
7. Set Relationship Checks Method Description issubset(other)
Checks if all items are in the other set issuperset(other)
Checks if it contains all of another set isdisjoint(other)
True if no common items
a = {1, 2}
b = {1, 2, 3}
print(a.issubset(b)) # True
print(b.issuperset(a)) # True
print(a.isdisjoint({4})) # True
8. Copy Set Method Description copy()
Returns a shallow copy of the set
a = {1, 2, 3}
b = a.copy()
print(b) # Output: {1, 2, 3}
Full List of Python Set Methods Method Description add()
Add an item clear()
Remove all items copy()
Return a shallow copy difference()
Return the difference of sets difference_update()
Remove common items discard()
Remove item if present intersection()
Return common items intersection_update()
Keep only common items isdisjoint()
Check for no common items issubset()
Check if current set is subset issuperset()
Check if current set is superset pop()
Remove a random item remove()
Remove specific item symmetric_difference()
Return elements in either set, not both symmetric_difference_update()
Update set with symmetric difference union()
Return union of sets update()
Add elements from another iterable