Python Set remove() Method
set.remove() deletes one specific value from a set.
Use it when you want to remove an item and you expect that item to be present. If the item is missing, Python raises a KeyError.
Quick example
colors = {"red", "blue", "green"}
colors.remove("blue")
print(colors)
Possible output:
{'red', 'green'}
Use remove() to delete a value from a set. If the value is not in the set, Python raises a KeyError.
What set.remove() does
set.remove():
- Removes one specific value from a set
- Changes the original set in place
- Does not return a new set
- Raises
KeyErrorif the value is missing
This means the set itself is updated. You do not need to assign the result to a new variable.
Basic syntax
my_set.remove(value)
my_setis the set you want to changevalueis the item to remove- Use this method when you expect the item to exist
Here is a small example:
numbers = {1, 2, 3}
numbers.remove(2)
print(numbers)
Possible output:
{1, 3}
Simple example
fruits = {"apple", "banana", "orange"}
fruits.remove("banana")
print(fruits)
Possible output:
{'apple', 'orange'}
This removes "banana" from the set.
A set does not keep items in a fixed order, so your printed output may appear in a different order. If you are new to sets, see how to create a set in Python.
What happens if the item is not found
If the value is not in the set, remove() raises a KeyError.
colors = {"red", "blue", "green"}
colors.remove("yellow")
Result:
KeyError: 'yellow'
This happens because "yellow" is not in the set.
This behavior is useful when a missing value should be treated as a real problem. For example, if your program assumes the item must be there, remove() helps you notice the mistake immediately.
If your code is failing with this error, see KeyError in Python: causes and fixes.
How remove() is different from discard()
remove() and discard() are similar, but they behave differently when the item is missing.
remove()
- Removes the item if it exists
- Raises an error if the item is missing
letters = {"a", "b", "c"}
letters.remove("d")
This raises:
KeyError: 'd'
discard()
- Removes the item if it exists
- Does nothing if the item is missing
letters = {"a", "b", "c"}
letters.discard("d")
print(letters)
Output:
{'a', 'b', 'c'}
Use:
remove()when missing values should be noticeddiscard()when you want to avoid errors
If you want a full method reference, see Python set discard() method.
Common beginner mistakes
Expecting remove() to return the updated set
remove() changes the original set and returns None.
Wrong:
colors = {"red", "blue", "green"}
new_colors = colors.remove("blue")
print(new_colors)
Output:
None
Correct:
colors = {"red", "blue", "green"}
colors.remove("blue")
print(colors)
Trying to remove a value that is not present
If the value is missing, you get a KeyError.
colors = {"red", "blue"}
colors.remove("green")
To check first, you can use in:
colors = {"red", "blue"}
if "green" in colors:
colors.remove("green")
Or use discard() if missing values are acceptable.
Confusing set.remove() with list.remove()
Sets do not store duplicates.
A list can contain repeated values, but a set cannot. That means set.remove() removes a value from a collection where each item is unique.
If you are working with lists and duplicates, you may also want to read how to remove duplicates from a list in Python.
Assuming sets keep item order
Sets are unordered collections.
After removing an item, do not expect the remaining items to stay in a specific order when printed.
FAQ
Does set.remove() return a new set?
No. It changes the original set and returns None.
What error does set.remove() raise?
It raises KeyError if the value is not in the set.
Should I use remove() or discard()?
Use remove() when the item should exist. Use discard() when missing items are acceptable.
Can set.remove() remove multiple items at once?
No. It removes one item at a time. For multiple items, use a loop or another set operation.