Python Set discard() Method

The set.discard() method removes a specific element from the set.

Syntax:

set.discard(element)

Parameters:

element: (Required.) The element to be deleted.

Return Value:

No return value.

The following demonstrates the set.discard() method.

Example: discard()
langs = {'Python','Go','C++','Java'}
langs.discard('C++')
print("The set after discard() is called: ",langs)
Output
The set after discard() is called:  {'Java', 'Go', 'Python'}

If the element passed as arguement is not present in the set, no error is thrown and the set remains unchanged.

Example: discard()
langs = {'Python','Go','C++','Java'}
langs.discard('PHP')
print("The set after discard() is called: ",langs)
Output
The set after discard() is called:  {'C++', 'Java', 'Go', 'Python'}