remove() and discard() in Python Sets


According to a survey, the most used programming language around the world is python. This displays the necessity of having the knowledge about different methods of programming used in python. Pythons stores all the programming data in different methods. Some of the different data types are Sets, List, Dictionary. In this article we are going to learn about python sets and how remove() and discard() functions are used in python sets.

Remove()

This function is particularly used to remove() one specific element of the tag. It removes the specified element from the set and then displays the manipulated output. The only limitation to this method is that it can remove only one element at a time from the specified data set. We can understand the use of remove() function using examples. Lets first see what is the basic syntax of remove() function:

set.remove(data) #This is the basic form in which the remove() method is used to remove elements 

Let’s take two different cases to learn how the remove() function would provide the output in two different cases.

Example 1: Presence of Element to Be Removed

Countries = {"India", "London", "Nigeria", "Australia", "Dubai", "Italy", "Kazakhstan", "Iran", "Turkey", "Switzerland"}  # The data set contains all the different names
Countries.remove("Australia")  #Specify using the remove() method the name to be removed from the data set
print(Countries)

Output

The output of the above code will be as:

{'Nigeria', 'Switzerland', 'Turkey', 'London', 'India', 'Dubai', 'Italy', 'Kazakhstan', 'Iran'}

Example 2: Absence of Element to Be Removed

Countries = {"India", "London", "Nigeria", "Australia", "Dubai", "Italy", "Kazakistan", "Iran", "Turkey", "Switzerland"}  # The data set contains all the different names other then Pakistan
Countries.remove("Pakistan")  #The remove() method displays an error when the element not present in datset is to be removed
print(Countries)  

Output

The output of the above code will be:

KeyError: 'Pakistan'  #There will be no output displayed and instead of it such an error will be displayed

Discard()

This method uses a different method of displaying output. In this method the output will never be displayed as error, if the element to be removed is not present in the data set then the data will be displayed as it is without any changes unlike remove() method where the output would be displayed as an error when the element is not present. The limitation of this method is same as the previous one that only one element can be removed at a time. We can understand it more clearly using the following code and example:

set.discard(data) #This syntax will always be common in which the element to be removed will be defined in the parentheses

Example 1: Presence of Element to Be Removed

Cities = {"Hyderabad", "Bangalore", "Mumbai", "Pune", "Ahmedabad", "Kolkata", "Nagpur", "Nashik", "Jaipur", "Udaipur", "Jaisalmer"} #Different Elements are present in the data set
Cities.discard("Kolkata") #If the element will be present in the data set it will be normally removed and the manipulated output will be displayed
print(Cities)

Output

The output of the above example will be as follows:

{'Jaisalmer', 'Hyderabad', 'Nagpur', 'Ahmedabad', 'Bangalore', 'Pune', 'Mumbai', 'Udaipur', 'Jaipur', 'Nashik'}

Example 2: Absence of Element to Be Removed

States = {"Hyderabad", "Bangalore", "Mumbai", "Pune", "Ahmedabad", "Kolkata", "Nagpur", "Nashik", "Jaipur", "Udaipur", "Jaisalmer"} #The element name to be discarded is not present in the data set
States.discard("Kumbhalgarh") #If the element will not be present in the data set then the output will display the same data set without any manipulation
print(States)

Output

The output of the above example will be as follow:

{'Nagpur', 'Jaipur', 'Nashik', 'Hyderabad', 'Ahmedabad', 'Udaipur', 'Pune', 'Jaisalmer', 'Kolkata', 'Bangalore', 'Mumbai'}

Difference Between Both the Methods

The removal of the selected element from the data set is the same eventual aim for both methods, but their methods and rationale differ. When using the replace() function, the result won't be presented if the element that has to be removed isn't present in the supplied data; instead, an error will be shown. However, when using the discard() method, things work differently. If the element to be removed is not present in the data when using the discard() function, the output will show the data exactly as it is without any modifications and will not show any errors in its place.

Thus both the method have their dedicated use and different methods of presenting the output and they are to be used in the program as per need. Like if you are unsure about the presence of any specific element in the data and don’t want any error to occur while running the code then use the discard() method so that even in the case when the element is not present, the output can be displayed without any error.

Conclusion

Python is a useful programming language used worldwide by programmers for different purposes. To be an efficient and successful programmer it is necessary to have knowledge about different methods of performing different tasks in less time. This is the only key to become a successful python developer and work efficiently and fast.

This article displays the different uses of the both the remove() and the discard() method along with their examples to make it more clear for the users and any reader can take the above article as reference to learn more about remove() and discard() method.

Updated on: 01-Aug-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements