Lesson 09
Set Methods — add(), remove(), discard(), pop(), update()
Mutate Python sets safely: add elements, remove with remove() vs discard(), pop arbitrary items, merge with update(), clear, and copy.
What you will learn
Mutating Sets · Safe Removal · Bulk Updates · Copy
A set is mutable, which means you can add and remove elements. The key difference from lists is that sets work with values, not positions.
| Method | Role |
|---|---|
add(x) |
Insert one value |
update(it) |
Merge many values |
remove(x) / discard(x) |
Delete by value |
pop() |
Remove arbitrary element |
clear() / copy() |
Empty or duplicate set |
Memory formula: Add → Remove → Update → Clear
After File 08 — Set Basics, you know how to create sets and dedupe. This lesson covers mutating methods — the first half of Python's 16 set methods. Set algebra comes in File 10.
Learning roadmap
1. add() — insert one value
Adds one element to the set. Duplicates are ignored.
s = {10, 20}
s.add(30)
print(s) # {10, 20, 30} — order may varyDuplicate values
s.add(20)
print(s) # unchanged — 20 already exists2. remove() vs discard()
Both remove a value, but they behave differently when the value is missing.
remove()
s = {10, 20, 30}
s.remove(10)
print(s) # {20, 30}If the value is missing:
s.remove(99) # KeyErrordiscard()
s = {10, 20, 30}
s.discard(99)
print(s) # {10, 20, 30} — no exception| Method | Missing value |
|---|---|
remove(x) |
KeyError |
discard(x) |
Silent (no error) |
Interview tip: Use
discard()when you are not sure the value exists.
Compare with list remove() in File 02 — lists also raise ValueError when the value is missing.
3. pop() — remove an arbitrary element
Unlike lists, sets have no first or last item.
s = {10, 20, 30, 40}
item = s.pop()
print(item) # e.g. 20 — unpredictable
print(s)Important: Never assume which value will be removed.
Empty set
empty = set()
empty.pop() # KeyError4. update() — merge multiple values
Adds all elements from another iterable — like extend() for lists but deduplicates automatically.
s = {10, 20}
s.update([30, 40, 30])
print(s) # {10, 20, 30, 40}Notice duplicate 30 appears only once.
update() accepts many iterables
s.update((50, 60))
s.update("AB")
print(s) # includes 50, 60, 'A', 'B'5. clear() — remove everything
Empties the set but keeps the same object.
s = {10, 20}
s.clear()
print(s) # set()6. copy() — independent set
Creates a new set object.
a = {1, 2, 3}
b = a.copy()
a.add(4)
print(a) # {1, 2, 3, 4}
print(b) # {1, 2, 3}Unlike aliasing (b = a), these are different objects.
del on sets
del s[0] is invalid — sets have no index. Only del s removes the variable name.
7. Method summary
| Method | Purpose | In-place |
|---|---|---|
add(x) |
Add one value | Yes |
update(it) |
Add many values | Yes |
remove(x) |
Delete value | Yes |
discard(x) |
Safe delete | Yes |
pop() |
Remove arbitrary value | Yes |
clear() |
Empty set | Yes |
copy() |
Create new set | No |
Mini project — student club management
A school has two clubs that need to merge memberships.
coding = {"Asha", "Ravi", "Meera"}
robotics = {"Ravi", "Kiran"}
coding.update(robotics) # merge robotics into coding
coding.discard("Unknown") # safe — no KeyError
coding.remove("Meera") # remove member
print(coding) # {'Asha', 'Ravi', 'Kiran'}Visual
Final set contains {'Asha', 'Ravi', 'Kiran'} — Ravi is not duplicated, Meera is removed.
One-page revision sheet
| Task | Syntax |
|---|---|
| Add one value | s.add(x) |
| Add many | s.update(iterable) |
| Remove value | s.remove(x) |
| Safe remove | s.discard(x) |
| Remove random | s.pop() |
| Empty set | s.clear() |
| Copy | s.copy() |
Common interview mistakes
Mistake 1 — using remove() when the value may not exist:
s.remove(99) # KeyError
s.discard(99) # correct — silentMistake 2 — assuming pop() removes the first element:
s.pop() # arbitrary element — sets are unorderedPractice questions
Test yourself before File 10 — Set Algebra:
- When should you choose
discard()overremove()? - Why doesn't
update([30, 40, 30])store two30s? - What happens if you call
pop()on an empty set? - Does
clear()delete the variable or just empty the set? - What is the difference between
copy()and simple aliasing (b = a)?
Knowledge check
Q1: discard() vs remove()?
Answer: Use discard() when the element might not exist — it fails silently instead of raising KeyError.
Q2: Why only one 30 after update([30, 40, 30])?
Answer: Sets store unique values only.
Q3: pop() on empty set?
Answer: KeyError.
Q4: clear() — variable or contents?
Answer: Empties the set object — the variable name still exists.
Q5: copy() vs b = a?
Answer: copy() creates a new set object; aliasing shares one object.
Loading playground…