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 String strip()
Python List Comprehension
Python Program to Display the multiplication Table
Python String islower()
Python Program to Sort Words in Alphabetic Order
Python next()
Python dict()
Python Program to Display Fibonacci Sequence Using Recursion
Python String istitle()
Python Program to Display Powers of 2 Using Anonymous Function
Python property()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Set update()
Python Matrices and NumPy Arrays
Python List index()
JavaScript Map and Set
Python object()
Python String endswith()
Python Set pop()
Python Program to Merge Two Dictionaries
Python pow()
Python Set difference_update()
Python Program to Print Hello world!
Python Global Keyword
Python complex()
Python Set isdisjoint()
Python Program to Print the Fibonacci sequence
Python Tuple
Python Program to Illustrate Different Set Operations
Python Dictionary setdefault()
Python Decorators
Python Program to Check If a String Is a Number (Float)