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 Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String endswith()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String rpartition()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python pass statement
Python Tuple index()
Python Global, Local and Nonlocal variables
Python next()
Python Program to Illustrate Different Set Operations
Python slice()
Python String lstrip()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Dictionary keys()
Python Dictionary copy()
Python __import__()
Python Program to Find Numbers Divisible by Another Number
Python Operator Overloading
Python Numbers, Type Conversion and Mathematics
Python Program to Find Factorial of Number Using Recursion
Python delattr()
Python Program to Create a Countdown Timer
Python String isspace()
Python super()
Python String strip()
Python Program to Check the File Size
Python Set copy()
Python Object Oriented Programming
Python String format_map()
Python id()
Python Generators