Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min

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

add remove discard update — set mutation path

1. add() — insert one value

Adds one element to the set. Duplicates are ignored.

add(30) inserts one unique value into set
python
s = {10, 20}

s.add(30)
print(s)  # {10, 20, 30} — order may vary

Duplicate values

python
s.add(20)
print(s)  # unchanged — 20 already exists

2. remove() vs discard()

Both remove a value, but they behave differently when the value is missing.

remove(99) KeyError vs discard(99) no error

remove()

python
s = {10, 20, 30}

s.remove(10)
print(s)  # {20, 30}

If the value is missing:

python
s.remove(99)  # KeyError

discard()

python
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.

pop() removes one arbitrary element from set
python
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

python
empty = set()
empty.pop()  # KeyError

4. update() — merge multiple values

Adds all elements from another iterable — like extend() for lists but deduplicates automatically.

Set A update list merges into {10,20,30,40}
python
s = {10, 20}

s.update([30, 40, 30])
print(s)  # {10, 20, 30, 40}

Notice duplicate 30 appears only once.

update() accepts many iterables

python
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.

clear() turns {10,20} into set()
python
s = {10, 20}

s.clear()
print(s)  # set()

6. copy() — independent set

Creates a new set object.

copy() — a.add(4) does not change b
python
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.

python
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

Club members after update remove and discard

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:

python
s.remove(99)   # KeyError
s.discard(99)  # correct — silent

Mistake 2 — assuming pop() removes the first element:

python
s.pop()  # arbitrary element — sets are unordered

Practice questions

Test yourself before File 10 — Set Algebra:

  1. When should you choose discard() over remove()?
  2. Why doesn't update([30, 40, 30]) store two 30s?
  3. What happens if you call pop() on an empty set?
  4. Does clear() delete the variable or just empty the set?
  5. 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…