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 the Size (Resolution) of a Image
Python Set clear()
Python Program to Merge Mails
Python Function Arguments
Python String lstrip()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python List sort()
Python Set difference()
Python int()
Python Set symmetric_difference_update()
Python Program to Find Numbers Divisible by Another Number
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Java Program to Implement Dijkstra’s Algorithm using Set
Python String zfill()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Matrices and NumPy Arrays
Python max()
Python *args and **kwargs
Python Program to Catch Multiple Exceptions in One Line
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Reverse a Number
Python Program to Randomly Select an Element From the List
Python String rpartition()
Python String upper()
Python String expandtabs()
Python Machine Learning - Sebastian Raschka
Python String isidentifier()
Node.js vs Python for Backend Development
Python String replace()
Python Program to Find Hash of File
Python Program to Copy a File
Python delattr()