Python Set discard() Method Tutorial

In this section, we will learn what the Set discard() method is and how to use it in Python.

What is Set discard() Method in Python?

The Python Set discard() method is used to remove an element from the target Set object.

Basically, this method takes one argument and that is the value we want to be removed from a set object.

Note: there’s another method called remove() which does the same task. But the difference is that the remove() method will return an error if the specified argument does not exist in the target Set object while the discard() method does nothing!

Python Set discard() Method Syntax:

set.discard(value)

Set discard() Method Parameter:

The method takes only one argument and that is the value we want to remove from the target Set object.

Set discard() method Return Value

The method does not return a value.

Example: using python set discard() method

set1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

set1.discard(15)

print(set1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

Leave a Reply