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 Find Sum of Natural Numbers Using Recursion
Python callable()
Python Program to Return Multiple Values From a Function
Python oct()
Python chr()
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Find the Factorial of a Number
Python Functions
Python Set issuperset()
Python Program to Transpose a Matrix
Python any()
Python Program to Print Output Without a Newline
Python Input, Output and Import
Python del Statement
Python Anonymous / Lambda Function
Python Program to Remove Punctuations From a String
Python datetime
Python Program to Get Line Count of a File
Python Program to Print the Fibonacci sequence
Python Program to Make a Flattened List from Nested List
Python Get Current time
Python String isalpha()
Python Dictionary
Python Errors and Built-in Exceptions
JavaScript Map and Set
Python String encode()
Python dir()
Node.js vs Python for Backend Development
Python for Loop
Python Program to Find Armstrong Number in an Interval
Python Set copy()
Python break and continue