- discard() is a python in-built function which is applicable only for the set data type
- it is used for remove the specified item from the set.
- Work of both remove() and discard() are same but the major difference between them is that remove give an error if item is not in the list but discard not give an error.
Syntax-
set.discard(item)
Example
set is {1,2,3}
then set.discard(2) will give {1,3}
program-
set1=set([1,2,3,4,5,"one","two","three"])
print("Set is ",set1)
set1.discard(2)
print("Set is",set1)
set1.discard("one")
print("Set is",set1)
set1.discard(3)
print("Set is",set1)
set1.discard("three")
print("Set is",set1)
Set is {1, 2, 3, 4, 5, 'three', 'one', 'two'}
Set is {1, 3, 4, 5, 'three', 'one', 'two'}
Set is {1, 3, 4, 5, 'three', 'two'}
Set is {1, 4, 5, 'three', 'two'}
Set is {1, 4, 5, 'two'}