Python Set discard method with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In this tutorial, we will learn about python set discard method.
Python Set Discard

discard method :

This method is used to remove single element ( passed as an argument ) from the set object. If we try to remove an element, which does not exist in the set object. It will do nothing unlike in case of remove method, which raises a KeyError in such cases.

Syntax : < Set Object >.discard( < Immutable Object > ) 


# create set object and assign it to variable s s = {1,2,3,4,5} # discard element 3 from the set object s.discard(3) # print all elements of set print(s) # discard element 6 from the set object s.discard(6) # print all elements of set print(s)

References :-

  1. discard method Docs

That’s all for Python Set discard method. If you liked it, please share your thoughts in comments section and share it with others too.