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 Set update()
Python Program to Display Fibonacci Sequence Using Recursion
Python Dictionary copy()
Python Keywords and Identifiers
Python Program to Merge Two Dictionaries
Python eval()
Python String zfill()
Python hex()
Python Program to Convert Celsius To Fahrenheit
Python open()
Python String isdigit()
Python enumerate()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python min()
Python compile()
Python Machine Learning - Sebastian Raschka
Python Anonymous / Lambda Function
Python delattr()
Python String translate()
Python Program to Check If Two Strings are Anagram
Python Input, Output and Import
Python Set difference()
Python input()
Python Program to Multiply Two Matrices
Python format()
Python String rsplit()
Python Program to Convert Decimal to Binary Using Recursion
Python isinstance()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Generators
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants