Python set.discard() Method

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

Definition

discard(item) removes item from a set if it is there. If it is not there, nothing happens: no error, no change. The set is changed in place.

That is the difference from remove(), which raises KeyError for a missing item. Choose discard() when you only care that the item is gone afterwards, not that it was there before.

set.discard() is an instance method of the built-in set type: call it on a set with dot notation, such as guests.discard(...). 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.discard(item)
CallWhat it does
{1, 2, 3}.discard(2)Returns None; the set becomes {1, 3}.
{1, 2, 3}.discard(9)Returns None; the set becomes {1, 2, 3}, unchanged: a missing item is not an error.
{"a"}.discard(["a"])Raises TypeError: a list cannot be looked up in a set because it is unhashable.

discard() never tells you whether it removed anything. If you need to know, compare len(s) before and after, or test item in s first.

Method signatures
python
set.discard(item)
# returns: None; item is no longer in the set
# raises: TypeError if item is unhashable (never KeyError)

Parameters

ParameterDescription
itemThe value to delete, matched by equality. It must be hashable, so a list raises TypeError. It is required: discard() with no argument raises TypeError as well.

Return Value

Return ValueDescription
NoneAlways None, whether the item was removed or not.

Python Examples

Example 1 — Cancel RSVPs Without Errors

Discard a list of names, some of which never signed up.

python
rsvp = {"Mia", "Noah", "Zoe", "Liam"}

for name in ["Noah", "Ava", "Zoe"]:
    rsvp.discard(name)

print(sorted(rsvp))
print(len(rsvp))

How It Works

  1. Known names are removed. Noah and Zoe were in the set, so both are deleted.
  2. Unknown names are ignored. Ava never signed up, so discarding her changes nothing and raises nothing.
  3. Two guests remain. Mia and Liam are left, and sorted() prints them alphabetically.

Example 2 — Compare discard() With remove()

Delete a code that does not exist with both methods and watch which one complains.

python
codes = {"A10", "B20"}

codes.discard("C30")
print(sorted(codes))

try:
    codes.remove("C30")
except KeyError:
    print("remove() raised KeyError")

print(codes.discard("A10"))
print(sorted(codes))

How It Works

  1. Quiet for a missing code. discard("C30") finds nothing to delete and leaves both codes in place.
  2. Loud for the same code. remove("C30") raises KeyError, which the except block turns into a message.
  3. No result either way. Discarding "A10" prints None, because discard() only changes the set.
  4. One code left. After "A10" is gone, only "B20" remains.

Example 3 — Log Out Blocked Users and Count Them

Discard a block list that contains a duplicate and an unknown name, then work out how many users really left.

python
online = {"admin", "guest", "kim", "root", "sam"}
blocked = ["root", "admin", "nobody", "root"]

before = len(online)
for user in blocked:
    online.discard(user)

print(before - len(online), "users removed")
print(sorted(online))
print("root" in online)

How It Works

  1. Measure before and after. The size is 5 before the loop, so the difference afterwards tells how many items were really deleted.
  2. A repeated name is harmless. "root" is discarded twice; the second time it is already gone and nothing happens.
  3. Only two matches. "root" and "admin" were online, and "nobody" never was, so exactly two users are removed.
  4. The result. Guest, kim, and sam stay online, and "root" in online is False.

Frequently Asked Questions

Nothing. It does not raise an error and does not change the set, which is the main difference from remove().
Use discard() when a missing item is fine. Use remove() when the item must be there, so that a mistake shows up as a KeyError.
It returns None either way. Check item in s before the call, or compare len(s) before and after.
No. The item must be hashable, so discard([1]) raises TypeError. Discard a tuple, or use difference_update() to discard many values.
Not safely. Changing the set size while looping over it raises RuntimeError, so loop over list(s) or sorted(s) instead.

Next: set.intersection()

Return a new set with only the items found in the set and in every other iterable.

intersection() →

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