Set Methods
4 min read ·
Python sets come with powerful built-in methods that allow you to add, remove, compare, and combine sets efficiently.
Because sets show unique and unordered data, these methods are widely used in data cleaning, comparisons, and logic building.
Below is a complete, method-by-method explanation, with intro, syntax, and example for each.
add()
What it does
Adds one element to the set.
Syntax
Example
update()
What it does
Adds multiple elements from an iterable.
Syntax
Example
remove()
What it does
Removes a specified element.
Raises error if element not found.
Syntax
Example
discard()
What it does
Removes element without raising error if missing.
Syntax
Example
pop()
What it does
Removes and returns a random element.
Syntax
Example
clear()
What it does
Removes all elements from the set.
Syntax
Example
union()
What it does
Returns a new set containing all unique elements.
Syntax
Example
intersection()
What it does
Returns elements common to both sets.
Syntax
Example
difference()
What it does
Returns elements in first set but not in second.
Syntax
Example
symmetric_difference()
What it does
Returns elements not common to both sets.
Syntax
Example
intersection_update()
What it does
Updates the set keeping only common elements.
Syntax
Example
difference_update()
What it does
Removes elements found in another set.
Syntax
Example
symmetric_difference_update()
What it does
Keeps only elements not common.
Syntax
Example
issubset()
What it does
Checks if one set is a subset of another.
Syntax
Example
issuperset()
What it does
Checks if one set contains another set.
Syntax
Example
isdisjoint()
What it does
Checks if two sets have no common elements.
Syntax
Example
All Set Methods – Summary Table
| Method | Purpose |
|---|---|
add() | Add one element |
update() | Add multiple elements |
remove() | Remove element (error if missing) |
discard() | Remove safely |
pop() | Remove random element |
clear() | Remove all |
union() | Combine sets |
intersection() | Common elements |
difference() | Unique to first |
symmetric_difference() | Non-common |
issubset() | Subset check |
issuperset() | Superset check |
isdisjoint() | No common elements |