Methods and Functions

Python Set discard()

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:

 

  1. 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}

  1. 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}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.