The built-in discard()
method in Python is used to remove a specific item from a set.
It is important to note that this method differs from the remove()
method, which does the same thing. However, unlike remove()
, discard()
will not raise an error if the specified item does not exist in the set that calls the method.
set.discard(item)
This method requires a value that will be removed from the set to be passed as a parameter.
The return value of discard()
is None
.
x = {'Cats', 'Dogs', 'Elephants', 'Seals'}x.discard('Cats')print(x)
#Passing a value that does not exist in the setx = {'Cats', 'Dogs', 'Elephants', 'Seals'}x.discard('Tigers')print(x)