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 Shallow Copy and Deep Copy
Converting Between an Array and a Set in Java
Python enumerate()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python String rfind()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Randomly Select an Element From the List
Python Set symmetric_difference_update()
JavaScript Map and Set
Python Program to Iterate Over Dictionaries Using for Loop
Python chr()
Python int()
Python Set pop()
Python Program to Print Colored Text to the Terminal
Python String strip()
Python object()
Python Data Structures and Algorithms - Benjamin Baka
Python String replace()
Python Program to Check If Two Strings are Anagram
Python Global Keyword
Python repr()
Python Operator Overloading
Python String casefold()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Check if a Number is Positive, Negative or 0
Python Variables, Constants and Literals
Python bool()
Python List count()
Python Program to Add Two Matrices
Python Object Oriented Programming
Python all()