Python Set discard() Method
set.discard() removes one item from a set.
Use it when you want to remove a value safely, even if that value might not be in the set. Unlike set.remove(), discard() does not raise an error for a missing item.
Quick example
colors = {"red", "blue", "green"}
colors.discard("blue")
print(colors)
colors.discard("yellow") # no error if missing
print(colors)
Possible output:
{'red', 'green'}
{'red', 'green'}
Use discard() when you want to remove a value from a set safely, even if that value may not exist.
What set.discard() does
discard():
- Removes one item from a set
- Does nothing if the item is not in the set
- Changes the original set in place
- Returns
None
This means the set itself is updated. Python does not create a new set.
If you are new to sets, see what a set is in Python or how to create a set.
Basic syntax
my_set.discard(value)
my_setis the set you want to changevalueis the item you want to remove- The set must already exist
- The value must be a valid set element type
Example
numbers = {1, 2, 3, 4}
numbers.discard(3)
print(numbers)
Output:
{1, 2, 4}
What makes discard() different
discard() is similar to other set methods, but it behaves differently in an important way.
- Unlike
remove(),discard()does not raiseKeyErrorwhen the item is missing - Unlike
pop(),discard()removes a specific item - It is useful when you are not sure whether the item exists
discard() vs remove()
items = {"apple", "banana"}
items.discard("orange") # no error
print(items)
items.remove("banana") # removes existing item
print(items)
Output:
{'apple', 'banana'}
{'apple'}
If you call remove() with a missing value, Python raises an error. See KeyError in Python for more help with that case.
What the method returns
discard() returns None.
That means you should not write code like this:
colors = {"red", "blue", "green"}
result = colors.discard("blue")
print(result)
Output:
None
The changed set is still stored in colors:
colors = {"red", "blue", "green"}
colors.discard("blue")
print(colors)
Output:
{'red', 'green'}
Simple example
Here is a basic example that removes an existing item.
fruits = {"apple", "banana", "orange"}
print("Before:", fruits)
fruits.discard("banana")
print("After:", fruits)
Output:
Before: {'banana', 'apple', 'orange'}
After: {'apple', 'orange'}
The order of items in a set may look different on your computer because sets are unordered.
Example with a missing item
One of the most useful features of discard() is that it does not fail if the item is missing.
fruits = {"apple", "banana", "orange"}
fruits.discard("grape")
print(fruits)
Output:
{'banana', 'apple', 'orange'}
No error happens here.
This is helpful in beginner code because you do not need to check first:
if "grape" in fruits:
fruits.remove("grape")
With discard(), you can simply write:
fruits.discard("grape")
When to use discard()
Use discard() when:
- Missing values are normal
- You want simple, safe removal
- You do not want to write an existence check first
It is a good choice when your program does not care whether the value was already missing.
Common mistakes
Here are some common beginner mistakes with discard().
Expecting discard() to return the changed set
This is wrong:
numbers = {1, 2, 3}
numbers = numbers.discard(2)
print(numbers)
numbers becomes None because discard() returns None.
Use this instead:
numbers = {1, 2, 3}
numbers.discard(2)
print(numbers)
Confusing discard() with remove()
If you want to avoid errors for missing values, use discard().
letters = {"a", "b", "c"}
letters.discard("z") # safe
print(letters)
If you use remove("z"), Python raises a KeyError.
Trying to use discard() on a list instead of a set
discard() is a set method, not a list method.
Wrong:
items = [1, 2, 3]
items.discard(2)
Use a set if you need discard():
items = {1, 2, 3}
items.discard(2)
print(items)
Passing an unhashable value such as a list
Set items must be hashable. A list is not hashable.
This causes an error:
data = {1, 2, 3}
data.discard([2])
Use a valid set element type instead, such as a number, string, or tuple.
Helpful debugging checks
If discard() is not working as expected, these quick checks can help:
print(my_set)
print(type(my_set))
print(value in my_set)
help(set.discard)
These checks help you confirm:
- What is currently in the set
- Whether the variable is really a set
- Whether the value exists before removal
- How Python documents the method
FAQ
What happens if the item is not in the set?
Nothing happens. discard() does not raise an error when the item is missing.
What does set.discard() return?
It returns None. It changes the original set instead of creating a new one.
What is the difference between discard() and remove()?
Both remove items from a set. remove() raises KeyError if the item is missing, but discard() does not.
Can I use discard() with lists or dictionaries?
No. discard() is a set method, so it works on set objects.