In this Python tutorial, we’ll learn about Python set discard method in detail. In order to better understand the usage of this method, we’ll go through multiple Python code examples.
Introduction: Python Set Discard Method
This method is used to remove a specific item from Python set. We’ve learned about Python set remove method in our previous article. Click here to learn about remove method in detail.
Difference between these two methods are that discard method does not raise any exception if the item to be removed is not present in set while remove method does raise an exception.
Let’s now first understand its syntax and then we’ll practically implement this method.
Syntax of discard() Method
set.discard(item)
- This method takes one argument. Item to be removed from the existing set is passed to this method as an argument.
- None is returned by this method. It just removes the specified item from set, if that item is present inside set.
- The discard() method does not raise an exception if the item to be removed is not present in the specified Python set.
Implementing Python Set Discard Method (Detailed Examples)
Don’t skip any of the below examples in order to get a detailed understanding of discard method of Python set.
Example 1: Remove an Item From Set Using discard() Method
setV={5,4,3,2,1} setV.discard(3) print(setV)
Output
{1, 2, 4, 5}
- First, we’ve created a simple Python set.
- Then we’ve applied discard method on it. Inside this method, we’ve specified item(3) to be removed from the existing set.
- Finally, we’ve printed the updated set and we can see that the item(3) has been removed successfully.
Let’s remove some other items from this set. See below:
setV.discard(1) print(setV) setV.discard(4) print(setV)
Output
{2, 4, 5} {2, 5}
Example 2: Remove Tuple Inside Set Using discard() Method
We can remove a tuple inside set as well using Python set discard method. See below:
setV={'a','b',(3,4,'n'),5.6} setV.discard((3,4,'n')) print(setV)
Output
{'b', 'a', 5.6}
We’ve created a Python set and and we’ve passed a tuple to this set as an item. By using discard() method, we’ve successfully removed the specified tuple from set.
Example 3: When Item To Be Removed Is Not Found
b={'a','b','c','d'} b.discard('z') print(b)
Output
{'a', 'b', 'd', 'c'}
We can see that no exception has been raised as the item to be removed has not been found in the specified Python set.
Example 4: Return Value of Python Set Discard Method
a={345,'Zee','Usman',4.6} retVal=a.discard(4.6) print('The return value is: ', retVal) print('The updated set is : ',a)
Output
The return value is: None The updated set is : {345, 'Usman', 'Zee'}
We’ve removed an item from existing Python set and have stored the returned value inside a variable. For demonstration, we’ve printed its value and it shows none. We also have displayed the updated set.
We can see that the discard() method does not return anything. It just updates the existing set by removing the specified item from it, if that item exists inside set.
Example 5: Discard Items From Python List Of Sets Using For Loop
setLst=[{'a','h'},{3,2,9},{55,77,232},{(3,4),7.8,5.44}] removeItemsLst=['h',8,232,(3,4)] for var1,var2 in zip(setLst,removeItemsLst): var1.discard(var2) print(setLst)
Output
[{'a'}, {9, 2, 3}, {77, 55}, {5.44, 7.8}]
- First, we’ve created a list of Python sets.
- Secondly, we’ve created a list of items that we want to remove from sets.
- We’ve used Python for loop which’ve two variables. First will store items of list of sets (setLst) and the second variable will store items of second list (removeItemsLst). It depends on the order in which they are used. We can use this (removeItemsLst, setLst). Now in this case, first variable will store items of removeItemsLst and second variable will store items of setLst. Click here to learn more about it.
- After that, we’ve used discard method which will remove specific item from the specified set of list.
- Finally, we’ve printed the updated Python list of sets.
So this is how we can easily use Python set discard method in our Python programs. Hope you’ve learned a lot from this tutorial. If so, then feel free to spread this post among other Python developers.
Do ask if you still have any queries related to the implementation of Python set discard method. We’ll be happy to clear all your doubts.
Conclusion
In conclusion, now we’ve an in-depth practical knowledge of how to properly make use of Python set discard method. Don’t forget to leave your informative feedback on this post.
You can discover more concepts about Python programming in this site. Thank you for reading this article.