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 List remove()
Python Program to Multiply Two Matrices
Python Program to Represent enum
Python Program to Check if a Number is Odd or Even
Python Program to Display Fibonacci Sequence Using Recursion
Python Dictionary pop()
Python map()
Python String isdigit()
Python Program to Get the Full Path of the Current Working Directory
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String isalnum()
Python Exception Handling Using try, except and finally statement
Python Set difference()
Python Dictionary
Python bool()
Python vars()
Python Program to Find HCF or GCD
Python Namespace and Scope
Python List pop()
Python Set pop()
Python if...else Statement
Python Program to Print Hello world!
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python list()
Python Set copy()
Python Program to Check If Two Strings are Anagram
Python String format()
Python String translate()
Python delattr()
Python filter()
Python dir()
Python Dictionary items()