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 property()
Python List clear()
Python @property decorator
Python int()
Python Dictionary values()
Python Decorators
Python globals()
Python Data Structures and Algorithms - Benjamin Baka
Python String format()
Python Recursion
Python sorted()
Deep Learning in Python - LazyProgrammer
Python File I/O Operation
Python bytearray()
Python String rstrip()
Python Program to Extract Extension From the File Name
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Iterate Through Two Lists in Parallel
Python String isalnum()
Python String lstrip()
Python Program to Check if a Number is Odd or Even
Python Program to Create a Long Multiline String
Python Dictionary popitem()
Python round()
Python Set isdisjoint()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Copy a File
Python Program to Check Prime Number
Python String isprintable()
Python Program to Find the Size (Resolution) of a Image
Python Tuple
Python Program to Sort a Dictionary by Value