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 complex()
How to Get Started With Python?
Python Program to Find the Size (Resolution) of a Image
Python Program to Find the Largest Among Three Numbers
Python Set copy()
Python Set union()
Python Program to Differentiate Between del, remove, and pop on a List
Python for Loop
Python Program to Find Numbers Divisible by Another Number
Python String lstrip()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Display Powers of 2 Using Anonymous Function
Python property()
Python min()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Calculate the Area of a Triangle
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python time Module
Python Program to Convert Two Lists Into a Dictionary
Python if...else Statement
Python String isdigit()
Python eval()
Python Program to Reverse a Number
Python Multiple Inheritance
Python Program to Find the Square Root
Python Dictionary copy()
Deep Learning with Python - Francois Chollet
Python staticmethod()
Python Data Types
Python Program to Get the Last Element of the List
Python Program to Print all Prime Numbers in an Interval
Python Dictionary values()