Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Set discard() Method

Python Set discard() Method

Python3 Lists Python Sets


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

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'}

Python3 Lists Python Sets