Removing an Element from a Set - remove() v/s discard() - Python
Removing an element from a Set In any data structure or data set, adding or removing the data is essential. Set is one of the most important data structures in Python. Set is an unordered collection of data and does not hold duplicate data, this makes it different from the other data structures like List and Tuple. In this post, Let's take a look at couple of different ways of removing an element from a Set. Using 'remove()' method Using 'discard()' method The purpose of both these methods is to remove an element (passed) from a set. So, What makes these methods different from each other? Read on to find out. remove() Syntax set.remove(<element_to_be_removed>) remove() method accepts an element to be removed as an argument and removes the corresponding element from the set. One thing to note here is that the element passed as an argument to remove method must present in the set . If the element is not found in the set, this method returns KeyError. 1 2 3 n...