104
Python Set discard() Method
Python discard() method discards or remove the elememt from the set. This method does not return anything, even no error if the elememt is not present. It takes a parameter which is an elememt to be removed. The method signature is given below.
Signature
Parameters
elem: element to be deleted.
Return
It returns None.
Let’s see some examples of discard() method to understand it’s functionality.
Python Set discard() Method Example 1
A simple example to use discard method to remove an element.
Output:
{1, 2, 3, 4, 5} {1, 3, 4, 5}
Python Set discard() Method Example 2
If the element is not present it returns none to the caller method.
Output:
{1, 2, 3, 4, 5} None
Python Set discard() Method Example 3
An example where we are implementing this method into a program. It removes all odds elements.
Output:
{1, 2, 3, 4, 5} {2, 4}
Next TopicPython Set