Home >>Python Set Methods >Python Set discard() Method

Python Set discard() Method

Python Set discard() Method

Python set discard() method is used to remove the specified item from the given input set. It is different from the remove() method because the remove() method will give an error if the specified item does not exist but this method will not.

Syntax:
set.discard(value)

Parameter Values

Parameter Description
value This is a required parameter. It defines the item to search for and remove.
Here is an example of Python discard() method:

thisset = {"apple", "banana", "mango"}
thisset.discard("banana")
print(thisset)

Output:
{'apple', 'mango'}

No Sidebar ads