Free cookie consent management tool by TermsFeed Python Set - discard | Pythontic.com

Python Set - discard

Method signature:

discard(element_in)

Method Overview:

  • The discard() method removes an element from a Python set if the element is present in the set.
  • Unlike the remove() method of the set class, the discard() method does not raise a KeyException if the element is not found. 

Parameters:

element_in : The element that is to be removed from a Python set.

Example:

# Example Python program that removes an element from a set
# using the method discard()
cars = {"Fossil fuelled", "Electric car", "Hydrogen car"}
cars.discard("Fossil fuelled")
print(cars)

Output:

{'Electric car', 'Hydrogen car'}

 


Copyright 2025 © pythontic.com