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 sleep()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python List extend()
Python Program to Split a List Into Evenly Sized Chunks
Python String translate()
Python Dictionary
Python Program to Find HCF or GCD
Python Program to Get the Class Name of an Instance
Python String zfill()
Python String partition()
Python Iterators
Python Program to Sort a Dictionary by Value
Python Objects and Classes
Python hash()
Python Tuple
Python Program to Compute the Power of a Number
Python String upper()
Python Machine Learning - Sebastian Raschka
Python String ljust()
Python Program to Create Pyramid Patterns
Python Program to Find Armstrong Number in an Interval
Python Inheritance
Python String rindex()
Python String istitle()
Python List index()
Python Package
Python staticmethod()
How to get current date and time in Python?
Python zip()
Python Operators
Python String lower()
Python Dictionary fromkeys()