Python | remove() and discard() in Sets

• Difficulty Level : Basic
• Last Updated : 21 Mar, 2023

In this article, we will see how to remove an element in a set, using the discard() and remove() method. We will also learn the difference between the two methods, although both of them produce the same results. Examples:

```Input : set = ([10, 20, 26, 41, 54, 20])
Output : {41, 10, 26, 54}

Input : set = (["ram", "aakash", "kaushik", "anand", "prashant"])
Output : {'ram', 'prashant', 'kaushik', 'anand'}```

Method 1: Use of discard() method

The built-in method, discard() in Python, removes the element from the set only if the element is present in the set. If the element is not present in the set, then no error or exception is raised and the original set is printed. If the element is present in the set:

Python3

 `# Python program to remove random elements of choice``# Function to remove elements using discard()``def` `Remove(sets):``    ``sets.discard(``20``)``    ``print` `(sets)``    ` `# Driver Code``sets ``=` `set``([``10``, ``20``, ``26``, ``41``, ``54``, ``20``])``Remove(sets)`

Output:

`{41, 10, 26, 54}`

The time complexity of this program is O(1) because the discard() method in Python’s built-in set class has a constant time complexity.

The auxiliary space complexity of this program is also O(1) because it only uses a single set object, sets, and a few local variables to execute.

If the element is not present in the set:

Python3

 `# Python program to remove random elements of choice``# Function to remove elements using discard()``def` `Remove(sets):``    ``sets.discard(``21``)``    ``print` `(sets)``    ` `# Driver Code``sets ``=` `set``([``10``, ``20``, ``26``, ``41``, ``54``, ``20``])``Remove(sets)`

Output:

`{41, 10, 26, 20, 54}`

The time complexity of this program is O(1) since the discard() method of set in Python has an average time complexity of O(1).

The auxiliary space used by this program is also O(1) because it only uses a constant amount of extra space to hold the input set and a constant value passed to the discard() method.

Method 2: Use of remove() method

The built-in method, remove() in Python, removes the element from the set only if the element is present in the set, just as the discard() method does but If the element is not present in the set, then an error or exception is raised. If the element is present in the set:

Python3

 `# Python program to remove random elements of choice``# Function to remove elements using remove()``def` `Remove(sets):``    ``sets.remove("aakash")``    ``print` `(sets)``    ` `# Driver Code``sets ``=` `set``(["ram", "aakash", "kaushik", "anand", "prashant"])``Remove(sets)`

Output:

`{'ram', 'anand', 'prashant', 'kaushik'}`

Time complexity: O(1) for the removal operation, O(n) for printing the resulting set where n is the number of remaining elements in the set.
Auxiliary space: O(1) since the function only modifies the given set in place and does not create any additional data structures.

If the element is not present in the set:

Python3

 `# Python program to remove random elements of choice``# Function to remove elements using remove()``def` `Remove(sets):``    ``sets.remove("gaurav")``    ``print` `(sets)``    ` `# Driver Code``sets ``=` `set``(["ram", "aakash", "kaushik", "anand", "prashant"])``Remove(sets)`

Output:

`No Output`

Error:

```Traceback (most recent call last):