Account
Categories

Python Set discard() Method


Definition

When you want to delete a value, the discard() method checks for the value in the set. If it finds the value, it deletes it; otherwise, it does nothing, and no error occurs.

Now you will clearly understand the method through the flowchart.

s = {10, 20}

discard(50)

50 (not in set)
   │
   ▼
{10, 20}
   │
   ▼
NO CHANGE

Result:
{10, 20}

Syntax:

set_name.discard(element)

Example:

s = {10, 20}
s.discard(50)
print(s)

Output:

Exercise

Create a set s = {5, 6} and discard 10 from it.