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:
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Check Leap Year
Python String startswith()
Python Tuple index()
Python Program to Find the Largest Among Three Numbers
Python Program to Capitalize the First Character of a String
Python dir()
Python frozenset()
Python Set union()
Python Program to Sort a Dictionary by Value
Python round()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Find the Sum of Natural Numbers
Python while Loop
Python String lower()
Python Closures
Python String rsplit()
Python Namespace and Scope
Python Program to Find ASCII Value of Character
Python int()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Dictionary clear()
Python Program to Get the Last Element of the List
Python Program to Check If Two Strings are Anagram
Python Type Conversion and Type Casting
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Modules
Python Program to Make a Flattened List from Nested List
Python Program to Parse a String to a Float or Int
Python bool()
Python Statement, Indentation and Comments