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 Decorators
Python Package
Python Functions
Converting Between an Array and a Set in Java
Python Set intersection()
Python Set pop()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Remove Punctuations From a String
Python String rfind()
Deep Learning in Python - LazyProgrammer
Python Tuple count()
Python String capitalize()
Python Program to Measure the Elapsed Time in Python
Python Set union()
How to Get Started With Python?
Python bytes()
Python Variables, Constants and Literals
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Check the File Size
Python str()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Find Numbers Divisible by Another Number
Python Program to Merge Two Dictionaries
Python String count()
Python Program to Create a Countdown Timer
Python print()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python eval()
Python Machine Learning - Sebastian Raschka
Python exec()
Python __import__()
Python Program to Print all Prime Numbers in an Interval