Table of Contents
The discard() method removes a specified element from the set (if present).
The syntax of discard() in Python is:
s.discard(x)
1. discard() Parameters
discard() method takes a single element x and removes it from the set (if present).
2. Return Value from discard()
discard() removes element x from the set if the element is present.
This method returns None (meaning, absence of a return value).
3. Example 1: How discard() works?
numbers = {2, 3, 4, 5}
numbers.discard(3)
print('numbers = ', numbers)
numbers.discard(10)
print('numbers = ', numbers)
Output
numbers = {2, 4, 5}
numbers = {2, 4, 5}
4. Example 2: How discard() works?
numbers = {2, 3, 5, 4}
# Returns None
# Meaning, absence of a return value
print(numbers.discard(3))
print('numbers =', numbers)
Output
None
numbers = {2, 4, 5}
Related posts:
Python Program to Get the Class Name of an Instance
Python Set update()
Python Strings
Python for Loop
Python Program to Get Line Count of a File
Python Program to Convert Celsius To Fahrenheit
Python strftime()
Python String index()
Python Inheritance
Python Program to Find the Factors of a Number
Python pass statement
Python Program to Sort Words in Alphabetic Order
Python Program to Transpose a Matrix
Python chr()
Python frozenset()
Python Errors and Built-in Exceptions
Python pow()
Python dir()
Python Program to Get File Creation and Modification Date
Python help()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Sort a Dictionary by Value
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Multiple Inheritance
Python Dictionary keys()
Python if...else Statement
Python Dictionary fromkeys()
Python Closures
Python Program to Shuffle Deck of Cards
Convert a Map to an Array, List or Set in Java
Python Global, Local and Nonlocal variables
Python Program to Measure the Elapsed Time in Python