Python set.remove() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

remove(item) deletes item from a set. The set is changed in place, and the method returns None.

If the item is not in the set, remove() raises KeyError. That makes it the strict choice: use it when a missing item would be a bug, and use discard() when a missing item is fine.

set.remove() is an instance method of the built-in set type: call it on a set with dot notation, such as stock.remove(...). No import is needed.

Syntax

Pass the item to delete, and call it on its own line, because the return value is always None.

python
s.remove(item)
CallWhat it does
{1, 2, 3}.remove(2)Returns None; the set becomes {1, 3}.
{1, 2, 3}.remove(9)Raises KeyError: 9 is not in the set.
{1, 2, 3}.remove(2.0)Returns None; the set becomes {1, 3}, because 2.0 equals 2 and so matches the item.

To remove an arbitrary item use pop(); to empty the whole set use clear(); to remove many items at once use difference_update().

Method signatures
python
set.remove(item)
# returns: None; item is no longer in the set
# raises: KeyError if item is not in the set

Parameters

ParameterDescription
itemThe value to delete. It is looked up by value, so 2.0 finds an item 2. It is required: remove() with no argument raises TypeError.

Return Value

Return ValueDescription
NoneAlways None. The set is one item smaller, or KeyError was raised and the set is unchanged.

Python Examples

Example 1 — Book a Seat From the Free List

Take one seat out of the free set, then try to book it a second time.

python
free = {"A1", "A2", "B1", "B2"}
booked = "B1"

free.remove(booked)
print(sorted(free))
print(booked in free)

try:
    free.remove(booked)
except KeyError:
    print(booked, "was not available")

How It Works

  1. One seat leaves. remove("B1") deletes that seat, so three seats are left in the set.
  2. Membership proves it. booked in free is now False, because the seat is gone.
  3. A second removal fails. The seat is no longer in the set, so remove() raises KeyError, which the except block catches.

Example 2 — Remove Expired Coupons Safely

Loop over a sorted copy while removing, then see the error you get when looping over the set itself.

python
coupons = {"SAVE5", "OLD1", "OLD2", "FREE"}

for code in sorted(coupons):
    if code.startswith("OLD"):
        coupons.remove(code)

print(sorted(coupons))
print(len(coupons))

try:
    for code in coupons:
        coupons.remove(code)
except RuntimeError:
    print("cannot change a set while looping over it")

How It Works

  1. Loop over a copy. sorted(coupons) builds a separate list, so removing from the set does not disturb the loop.
  2. Only the old codes go. The two codes starting with "OLD" are removed, leaving FREE and SAVE5.
  3. The set cannot be its own loop. Removing the first code changes the size, so the next step of the loop raises RuntimeError.

Example 3 — Drop Sold-Out Shoe Sizes

Remove sizes that must exist, then write a small helper that never raises.

python
def safe_remove(items, value):
    if value in items:
        items.remove(value)
        return True
    return False


sizes = {36, 38, 40, 42, 44}
for sold_out in [38, 44.0, 36]:
    sizes.remove(sold_out)
print(sorted(sizes))

print(safe_remove(sizes, 40), safe_remove(sizes, 40))
print(sorted(sizes))

How It Works

  1. Equal numbers match. The float 44.0 equals the int 44, so remove(44.0) deletes the item 44.
  2. Three removals. 38, 44, and 36 are taken out, leaving 40 and 42.
  3. Look before you leap. The helper tests membership first, so it returns True when it removed something and False when the value was missing.
  4. The second call finds nothing. After the first call removes 40, asking again returns False and no KeyError is raised.

Frequently Asked Questions

Both delete an item and return None. If the item is missing, remove() raises KeyError, while discard() quietly does nothing.
The item you asked for is not in the set. Check with item in s first, wrap the call in try/except KeyError, or use discard() instead.
No. Changing the size of a set during a for loop raises RuntimeError. Loop over a copy, such as for x in list(s), or build a new set with a comprehension.
Use pop(), which removes and returns an arbitrary item. To remove the smallest value, call min(s) first and then remove it.
Use difference_update() with an iterable, or the -= operator with another set. Both ignore items that are not present.

Next: set.symmetric_difference()

Return a new set with the items that are in exactly one of the two sets.

symmetric_difference() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful