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:
JavaScript Map and Set
Python pow()
Python strftime()
Python Functions
Python if...else Statement
Python Program to Measure the Elapsed Time in Python
Python Keywords and Identifiers
Python Global Keyword
Python Program to Find ASCII Value of Character
Python sleep()
Python chr()
Python print()
Python String rsplit()
Python Program to Get the Last Element of the List
Python Errors and Built-in Exceptions
Python String title()
Python String istitle()
Python Program to Make a Simple Calculator
Python String find()
Python String translate()
Python getattr()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Create Pyramid Patterns
Python Program to Differentiate Between type() and isinstance()
Python Program to Convert Bytes to a String
Python Program to Make a Flattened List from Nested List
Python Custom Exceptions
Python String casefold()
Python String maketrans()
Python Package
Python Dictionary pop()
Python Data Structures and Algorithms - Benjamin Baka