Sometimes users hope to remove certain elements from a set. With the help of the python discard function, users can discard an element if it is present in the set.
Syntax:
s.discard(x)
discard() Parameters
The python discard function takes a single parameter from the user. The parameter is ‘x.’ The element is discarded if it is present in the set.
Return Value from discard()
The return value of the discard() is the element that is removed. If the element mentioned by the user is not present in the set, then the discard method returns None. Â
Example 1: How does discard() works?
Look at the program given below to understand how the discard() in python works:
Source Code:
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}
Example 2: How does discard() works?
Look at the program given below to understand how the program returns a None value.Â
Source Code:
numbers = {2, 3, 5, 4}
# Returns None
print(numbers.discard(3))
print('numbers =', numbers)
Output
None
numbers = {2, 4, 5}
What is the difference between discard and remove in Python?
In Python, the discard() function removes a specified element from a Set. The remove() in python also removes an element from a set. However, there is a difference between remove() and discard().Â
When the specified element does not exist in the given set, the remove() function raises an error. Whereas the discard() method does not raise an error when the specified element does not exist in the set, and the set remains unchanged.
Examples:
- Discard()
Source Code 1: When the element is present in the set
def Remove(sets11):
    sets11.discard(20)
    print (sets11)
     Â
sets11 = set([10, 20, 26, 41, 54, 20])
Remove(sets11)
Output:
{41, 10, 26, 54}
Source Code 2: When the element is not present in the set
def Remove(sets11):
    sets11.discard(21)
    print (sets11)
     Â
# Driver Code
sets11 = set([10, 20, 26, 41, 54, 20])
Remove(sets11)
Output:
{41, 10, 26, 20, 54}
- remove()
Source Code 1: When the element is present in the set
def Remove(sets11):
    sets.remove("ash")
    print (sets11)
     Â
# Driver Code
sets11 = set(["rambo", "ash", "karthik", "andy", "priya"])
Remove(sets11)
Output:
{"rambo", "karthik", "andy", "priya"}
Source Code 2: When the element is not present in the set
def Remove(sets11):
    sets11.remove("gaurav")
    print (sets11)
     Â
# Driver Code
sets11 = set(["rambo", "ash", "karthik", "andy", "priya"])
Remove(sets11)
Output:
No Output
Error:
Traceback (most recent call last):
  File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 9, inÂ
    Remove(sets)
  File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 4, in Remove
    sets.remove("gaurav")
KeyError: 'gaurav'
How do you delete something in Python?
In python, users can remove items from a set using the discard() and the remove().
How do I remove multiple elements from a set in Python?
Users can remove multiple elements from a set in python using the difference_update()
Source Code:
set11 = {6, 4, 2, 7, 9}
 Â
print("Original set is : " + str(set11))
 Â
rem_ele1 = [2, 4, 8]
 Â
set11.difference_update(set(rem_ele1))
 Â
print("New set: " + str(set11))
Output:
Original set : {2, 4, 6, 7, 9}
New Set : {9, 6, 7}
Leave a Reply