![]() |
VOOZH | about |
Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value.
Example: In this example, we are removing the integer 3 from the set with discard() in Python.
Output
{1,2,4,5}set.discard(element)
Parameter
element - an item to remove from the set.Return Value
return - discard() method doesn't return any value.
In this example, we have a set and we use discard() to remove an existing integer "5" from the set using Python.
Output
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 6, 7, 8, 9}
In this example, we have a set and we use discard() to remove a non-existing integer "13" from the set using Python.
Output
{1, 2, 3, 4, 5, 6, 7, 8, 9}
resultant set : {1, 2, 3, 4, 5, 6, 7, 8, 9}
In this example, we have a set and we use discard() to remove an existing string "geek" from the set using Python.
Output
{1, 2, 'b', 'a', 8, 'geeksforgeeks', 'abc', 'geek'}
{1, 2, 'b', 'a', 8, 'geeksforgeeks', 'abc'}
In this example, we have a set and we use discard() to remove a non-existing string "geeks" from the set using Python.
Output
{1, 2, 'b', 'a', 8, 'geeksforgeeks', 'abc', 'geek'}
{1, 2, 'b', 'a', 8, 'geeksforgeeks', 'abc', 'geek'}
Note - To know the difference between discard() and remove() click here.