Python Set discard() Method
Description
The discard() method is used to remove the specified element from the set.
This method is different fromremove()method, becauseremove()method will raise an error when removing a non-existent element, whilediscard()method will not.
Syntax
The syntax of the discard() method is:
set.discard(value)
Parameters
- value -- Required, the element to be removed.
Return Value
None.
Example
Remove the element banana from the set:
Example 1
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)
The output is:
{'cherry', 'apple'}
Python Set