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 iter()
Python List append()
Python String isidentifier()
Python List sort()
Python repr()
Python exec()
Java Program to Implement Dijkstra’s Algorithm using Set
Python Numbers, Type Conversion and Mathematics
Python bytearray()
Python String join()
Python Dictionary update()
Python hash()
Python globals()
Python Program to Return Multiple Values From a Function
Python String rstrip()
Python dict()
Intelligent Projects Using Python - Santanu Pattanayak
Python String upper()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python divmod()
Python String rpartition()
Python __import__()
Python Recursion
Python String translate()
Python Tuple index()
Python Program to Find the Size (Resolution) of a Image
Python String expandtabs()
Python Program to Multiply Two Matrices
Python Program to Check If Two Strings are Anagram
Python range()
Python classmethod()
Python Program to Count the Number of Occurrence of a Character in String