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 Set union()
Python Program to Find the Square Root
Python Exception Handling Using try, except and finally statement
Python String isnumeric()
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Python Shallow Copy and Deep Copy
JavaScript Map and Set
APIs in Node.js vs Python - A Comparison
Python Set isdisjoint()
Python Program to Find HCF or GCD
Python Dictionary pop()
Python Program to Represent enum
Python String islower()
Python pow()
Python String upper()
Python list()
Python Program to Check if a Number is Odd or Even
Python Program to Get a Substring of a String
Deep Learning with Python - Francois Cholletf
Python Strings
Python Dictionary get()
Python timestamp to datetime and vice-versa
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python List
Python String isdigit()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Function Arguments
Python vars()
Python List pop()
Python Program to Find LCM
Python Iterators
Python super()