remove() method of set
remove(element) takes one argument .
element
: matching element to be removed from set
Returns : No return value.
my_set={'a','b','c','x','k'}
my_set.remove('x')
print(my_set)
Output is here
{'c', 'a', 'k', 'b'}
In above code x is deleted from my_set.
Removing not available element
This will generate error KeyError
my_set={'a','b','c','x','k'}
my_set.remove('y')
Difference between remove() and discard()
If the element is not present then discard() will not generate error.
my_set={'a','b','c','x','k'}
my_set.discard('y')
print(my_set)
Output
{'x', 'c', 'a', 'k', 'b'}
« All set methods « Questions with solutions on set discard()
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com