Python Set discard() Method

❮ Set Methods


Example

Remove "banana" from the set:

fruits = {"apple", "banana", "cherry"}

fruits.discard("banana")

print(fruits)
Try it Yourself »

Definition and Usage

The discard() method removes the specified item from the set.

This method is different from the remove() method, because the remove() method will raise an error if the specified item does not exist, and the discard() method will not.


Syntax

set.discard(value)

Parameter Values

Parameter Description
value Required. The item to search for, and remove

❮ Set Methods

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.