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 Print the Fibonacci sequence
Python String maketrans()
Python Set symmetric_difference()
Python List clear()
Python Program to Add Two Matrices
Python String isprintable()
Python set()
Python Data Types
Python Program to Parse a String to a Float or Int
Python Program to Safely Create a Nested Directory
Python Multiple Inheritance
Python Set intersection_update()
Python Program to Reverse a Number
Python String rjust()
Python Program to Count the Number of Digits Present In a Number
Python Program to Delete an Element From a Dictionary
Python globals()
Python Strings
Python locals()
Python Program to Differentiate Between del, remove, and pop on a List
Python Dictionary setdefault()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Directory and Files Management
Python String zfill()
Python String isupper()
Python Set update()
Python Object Oriented Programming
Python String count()
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Python delattr()
Python sorted()
Python Program to Find the Largest Among Three Numbers