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 String capitalize()
Python eval()
Python hash()
APIs in Node.js vs Python - A Comparison
Python Get Current time
Python Type Conversion and Type Casting
Python List count()
Python Program to Print Output Without a Newline
Python List append()
Python datetime
Python globals()
Python List
Python Program to Find Factorial of Number Using Recursion
Python Matrices and NumPy Arrays
Python Machine Learning - Sebastian Raschka
Python oct()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String splitlines()
Python id()
Python Set copy()
Python sorted()
Python sum()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Display the multiplication Table
Python Set clear()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Append to a File
Python String rpartition()
Python Program to Capitalize the First Character of a String
Python delattr()
Python List pop()
Python Strings