Python Set discard() Method

Python3 列表Python Set


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

Python3 列表Python Set

Other Extensions