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 bytes()
Python Directory and Files Management
Python Dictionary items()
Python String title()
Python Program to Trim Whitespace From a String
Converting Between an Array and a Set in Java
Python if...else Statement
Python any()
Python Dictionary
Python String splitlines()
Python enumerate()
Converting Between a List and a Set in Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Python Set union()
Python Set add()
Convert a Map to an Array, List or Set in Java
Python Program to Display the multiplication Table
Python format()
Python Matrices and NumPy Arrays
Python Objects and Classes
Python Program to Display Powers of 2 Using Anonymous Function
Python strptime()
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Check Leap Year
Python Variables, Constants and Literals
Python String rfind()
Python Modules
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Keywords and Identifiers
Python String isalnum()
Python Dictionary fromkeys()
Python Program to Extract Extension From the File Name