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