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:
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Set union()
Python Program to Display Powers of 2 Using Anonymous Function
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Python Program to Capitalize the First Character of a String
Python Set clear()
Python Program to Check If Two Strings are Anagram
Python String isalpha()
Python List clear()
Python Tuple index()
Python String format_map()
Python Set difference_update()
Python Anonymous / Lambda Function
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python vars()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python sum()
Python Tuple count()
Python Program to Iterate Through Two Lists in Parallel
Python List
Python Program to Sort Words in Alphabetic Order
Python Program to Count the Number of Digits Present In a Number
Python Program to Check the File Size
Python Program to Print Colored Text to the Terminal
Python Dictionary get()
Python Machine Learning - Sebastian Raschka
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Strings
Python Global, Local and Nonlocal variables
Python pass statement
Python Program to Check if a Number is Positive, Negative or 0
Python String lstrip()