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 Check the File Size
Python Program to Represent enum
How to get current date and time in Python?
Python Type Conversion and Type Casting
Python break and continue
Java Program to Implement Dijkstra’s Algorithm using Set
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python strftime()
Python String isprintable()
Python Program to Get the File Name From the File Path
Python Dictionary pop()
Python filter()
Python Program to Slice Lists
Python Program to Iterate Through Two Lists in Parallel
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Find Numbers Divisible by Another Number
Python super()
Python Program to Convert Two Lists Into a Dictionary
Python format()
Python Dictionary copy()
Python Program to Print Hello world!
Python String isnumeric()
Python id()
Python Program to Find the Sum of Natural Numbers
Python String rjust()
Python Program to Remove Duplicate Element From a List
Python Program to Create a Countdown Timer
Python Set issubset()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Set intersection()
Python List append()