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 Compute the Power of a Number
Python Dictionary items()
Python Program to Compute all the Permutation of the String
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Convert String to Datetime
Python String join()
Python String isdigit()
Python abs()
Python Program to Convert Bytes to a String
Python input()
Python Dictionary values()
Python issubclass()
Python Program to Check Armstrong Number
Python staticmethod()
Python String rfind()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Dictionary popitem()
Python Data Types
Python String rindex()
Python @property decorator
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Check if a Number is Odd or Even
Python Program to Get the Full Path of the Current Working Directory
Python Program to Find Hash of File
Python range()
Python Program to Differentiate Between type() and isinstance()
Python Program to Make a Flattened List from Nested List
Python set()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Check If a List is Empty
Convert a Map to an Array, List or Set in Java
Python Program to Display Powers of 2 Using Anonymous Function