Easy Tutorial
❮ Python Func Number Floor Ref Set Update ❯

Python Set discard() Method

Python Set


Description

The discard() method is used to remove a specified element from the set.

This method is different from the remove() method because the remove() method raises an error if the element does not exist, whereas the discard() method does not.

Syntax

The syntax for the discard() method is:

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

Python Set

❮ Python Func Number Floor Ref Set Update ❯