In Python, set is a built-in data type that represents an unordered collection of unique elements. Understanding and mastering set methods unlocks powerful tools for handling collections, performing mathematical set operations, and managing data efficiently.
Unlike lists or tuples, sets do not allow duplicates and do not maintain order. This uniqueness makes them ideal for tasks like membership testing, deduplication, and mathematical set operations such as union, intersection, and difference.

What Are Set Methods?
Set methods are functions that operate on set objects to add, remove, or manipulate elements. They enable you to:
- Modify sets by adding or removing elements.
- Combine sets using mathematical operations.
- Query sets for membership or relationships.
All set methods mutate the set in place except those that return new sets representing some operation result.
Core Set Methods Explained With Examples
Let's explore the most important set methods, grouped by their purpose.
1. Adding Elements
add(element): Adds a single element to the set.update(iterable): Adds multiple elements from any iterable (list, tuple, another set, etc.).
📌 Deep Dive: Adding Elements
fruits = {'apple', 'banana'}
fruits.add('orange')
print(fruits)
fruits.update(['mango', 'grape'])
print(fruits)
Note: Since sets are unordered, the printed order may vary.
2. Removing Elements
remove(element): Removes an element. RaisesKeyErrorif element is absent.discard(element): Removes an element if present; does nothing if absent (no error).pop(): Removes and returns an arbitrary element, raisingKeyErrorif set is empty.clear(): Removes all elements from the set.
📌 Deep Dive: Removing Elements
colors = {'red', 'green', 'blue'}
colors.remove('green') # Raises error if 'green' not present
print(colors)
colors.discard('yellow') # Does nothing, no error
print(colors)
popped = colors.pop()
print(f"Popped: {popped}")
print(colors)
colors.clear()
print(colors) # Empty set
⚠️ Important:
Use discard() if you want to avoid errors when removing an element that may not exist. remove() is stricter and will throw an exception if the element is missing.
3. Set Operations
These methods perform mathematical set operations, returning new sets or modifying the original.
union(other_set)or|: Returns a new set with all elements from both sets.intersection(other_set)or&: Returns a new set with elements common to both sets.difference(other_set)or-: Returns elements in the first set but not in the second.symmetric_difference(other_set)or^: Elements in either set but not in both.
In addition, there are in-place versions that modify the original set:
update(other_set)or|=: Adds elements from other set.intersection_update(other_set)or&=: Keeps only elements found in both sets.difference_update(other_set)or-=: Removes elements found in other set.symmetric_difference_update(other_set)or^=: Keeps elements in one set or the other but not both.
| Method | Operator |
|---|---|
| union() | | |
| intersection() | & |
| difference() | - |
| symmetric_difference() | ^ |
📌 Deep Dive: Set Operations
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Union:", a.union(b)) # {1, 2, 3, 4, 5, 6}
print("Intersection:", a.intersection(b)) # {3, 4}
print("Difference (a - b):", a.difference(b)) # {1, 2}
print("Symmetric Difference:", a.symmetric_difference(b)) # {1, 2, 5, 6}
# In-place difference update
a.difference_update(b)
print("After difference_update:", a) # {1, 2}
4. Querying Sets
There are also useful methods and operators to test relationships and membership:
issubset(other_set): Checks if current set is subset ofother_set.issuperset(other_set): Checks if current set contains all elements ofother_set.isdisjoint(other_set): ReturnsTrueif sets have no elements in common.inoperator: Tests if an element is in the set.
📌 Deep Dive: Querying Sets
x = {1, 2, 3}
y = {1, 2, 3, 4, 5}
print(x.issubset(y)) # True
print(y.issuperset(x)) # True
print(x.isdisjoint({6, 7})) # True
print(2 in x) # True
print(10 in x) # False
💡 Tip:
Using these querying methods helps write readable and intention-revealing code when testing set relations.
Advanced Set Methods
Python sets also provide additional methods useful in some contexts:
copy(): Returns a shallow copy of the set.frozenset(): This is not a method but a built-in type that creates an immutable version of a set (supports fewer methods).
📌 Deep Dive: Copying Sets
orig = {1, 2, 3}
copy_set = orig.copy()
copy_set.add(4)
print("Original:", orig)
print("Copy:", copy_set)
When to Use Sets and Their Methods
Sets shine when you need to:
- Eliminate duplicates quickly.
- Perform fast membership checks (
inoperator on sets is O(1) average time complexity). - Use mathematical set operations like unions and intersections on data collections.
- Modify collections dynamically with adds and removes without caring about order.
These operations are especially useful in data processing, filtering unique entries, and implementing algorithms that require set logic.
⚠️ Caution:
Remember, sets are unordered and unindexed. Trying to access elements by position or relying on order will not work. Use lists or tuples if order matters.
Summary of Most Common Set Methods
| Method | Description |
|---|---|
| add(elem) | Adds a single element |
| update(iterable) | Adds multiple elements |
| remove(elem) | Removes element, error if missing |
| discard(elem) | Removes element if present |
| pop() | Removes and returns an arbitrary element |
| clear() | Removes all elements |
| union(other) | Returns union of sets |
| intersection(other) | Returns common elements |
| difference(other) | Returns elements in this set not in other |
| symmetric_difference(other) | Elements in either set but not both |
| issubset(other) | Tests if subset |
| issuperset(other) | Tests if superset |
| isdisjoint(other) | Tests if no common elements |
| copy() | Returns a shallow copy |
Practice Exercise
Try creating two sets and experiment with the methods above. For example, create one set with some fruits, another with tropical fruits, then find their intersection and difference.
💡 Pro Tip:
Running these methods in an interactive Python shell or notebook lets you quickly understand how they operate.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which set method should you use if you want to remove an element without causing an error if it doesn't exist?
Question 2 of 2
What does the intersection() method return?
Loading results...