Python Set discard() Method
Description
The discard() method removes a specified element from a set.
This method differs from the remove() method, because the remove() method raises an error when removing an element that does not exist, while the discard() method does not.
Syntax
Syntax of the discard() method:
set.discard(value)
Parameters
- value -- required, the element to remove
Return Value
none.
Example
Remove the element banana from the set:
Example 1
fruits
= {
"
apple
"
,
"
banana
"
,
"
cherry
"
}
fruits
.
discard
(
"
banana
"
)
(
fruits
)
The output is:
{'cherry', 'apple'}
Python Sets