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 range()
Python callable()
Python String isspace()
Python Program to Check if a Number is Odd or Even
Python Program to Merge Mails
Python Dictionary update()
Python Program Read a File Line by Line Into a List
How to get current date and time in Python?
Python vars()
Python Set add()
Python String lower()
Python Functions
Python String find()
Python classmethod()
JavaScript Map and Set
Python chr()
APIs in Node.js vs Python - A Comparison
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Operator Overloading
Python Program to Create a Countdown Timer
Python Program to Iterate Through Two Lists in Parallel
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Check If Two Strings are Anagram
Python datetime
Python List clear()
Python Dictionary copy()
Python object()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Get the Class Name of an Instance
Python RegEx
Python Program to Find Numbers Divisible by Another Number
Deep Learning with Python - Francois Cholletf