Hello friends! Adding items to a set is easy, but sooner or later you will also need to take an item out. For that, Python gives us remove(). The remove() method deletes a specific item from a Python set. If the item is not in the set, it raises a KeyError. It changes the set in place and returns None. Let’s understand each of these points with examples.
set.remove(item)
Basic Example
nums = {10, 20, 30, 40}
nums.remove(20)
print(nums)
Output:
{40, 10, 30}
A set has no fixed order, so the items may be printed in a different order from the one you typed. That is normal.
That was easy. But what happens if you try to remove something that is not in the set at all?
Removing an Item That Is Not There
try:
nums.remove(99)
except KeyError as e:
print("KeyError:", e)
Output:
KeyError: 99
This is the main difference between remove() and discard(): discard() silently does nothing when the item is missing. Use remove() when a missing item would mean a bug that you want to notice.
To avoid the error, check first with the in operator:
s = {1, 2, 3}
if 5 in s:
s.remove(5)
print(s)
Output:
{1, 2, 3}
remove() Returns None
print(nums.remove(10))
print(nums)
Output:
None
{40, 30}
Do not write nums = nums.remove(10). That would replace your set with None.
Now let’s use remove() in a small real-life example.
Practical Example: Attendance
present = {"Riya", "Aman", "Kabir"}
present.discard("Neha") # Neha was never marked present, so no error
present.remove("Aman") # Aman must be in the set
print(sorted(present))
Output:
['Kabir', 'Riya']
Removing Items While Looping
Friends, this is a mistake almost every beginner makes at least once. You cannot change a set’s size while looping over it directly:
s = {1, 2, 3, 4, 5, 6}
try:
for x in s:
if x % 2 == 0:
s.remove(x)
except RuntimeError as e:
print("RuntimeError:", e)
Output:
RuntimeError: Set changed size during iteration
Loop over a copy (for x in list(s):) or, better, build a new set with a comprehension:
s = {1, 2, 3, 4, 5, 6}
s = {x for x in s if x % 2 != 0}
print(s)
Output:
{1, 3, 5}
Removing Several Items at Once
t = {1, 2, 3, 4, 5}
t -= {2, 4, 9}
print(t)
Output:
{1, 3, 5}
The -= operator (or difference_update()) removes every item of the other set, and does not complain about items like 9 that were never there.
Ways to Remove Items from a Set
remove() is not the only option. Here is a quick comparison of all the ways to remove items from a set:
| Method | What it does | Missing item? |
|---|---|---|
remove(x) |
Removes x | Raises KeyError |
discard(x) |
Removes x | Does nothing |
pop() |
Removes and returns an arbitrary item | KeyError if the set is empty |
clear() |
Removes everything | Not applicable |
-= / difference_update() |
Removes many items | Ignored |
So friends, to sum up: both remove() and discard() delete an item, but remove() raises a KeyError if the item is missing, while discard() does nothing. Since sets are unordered and have no indexes, you always remove items by value, not by position. And remember that remove() returns None, because it changes the set in place. Try these examples yourself and you will never be confused by them again.
More set methods: Set methods in Python · discard() · pop() · clear()