 # Python – remove() and discard() in Sets

## Python | remove() and discard() in Sets

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:

 `# 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}
```

If the element is not present in the set:

 `# 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}
```

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:

 `# 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'}
```

If the element is not present in the set:

 `# 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):
Remove(sets)
File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 4, in Remove
sets.remove("gaurav")
KeyError: 'gaurav'```

Last Updated on March 17, 2022 by admin

## Introduction to MatplotlibIntroduction to Matplotlib

Python | Introduction to Matplotlib Matplotlib is an amazing visualization library in Python for 2D

## How to compare two NumPy arrays?How to compare two NumPy arrays?

How to compare two NumPy arrays? This article focuses on the comparison done using NumPy

## Python bytearray() functionPython bytearray() function

Python | bytearray() function bytearray() method returns a bytearray object which is an array of given

## Oracle Database Connection in PythonOracle Database Connection in Python

Oracle Database Connection in Python Sometimes as part of programming, we required to work with

## Generating all possible Subsequences using RecursionGenerating all possible Subsequences using Recursion

Generating all possible Subsequences using Recursion Given an array. The task is to generate and

## Fastest way to Convert Integers to Strings in Pandas DataFrameFastest way to Convert Integers to Strings in Pandas DataFrame

Fastest way to Convert Integers to Strings in Pandas DataFrame Pandas – An open-source library

## Python – Remove all values from a list present in other listPython – Remove all values from a list present in other list

Python | Remove all values from a list present in other list Sometimes we need

## Python – Draw Star Using Turtle GraphicsPython – Draw Star Using Turtle Graphics

Python – Draw Star Using Turtle Graphics In this article, we will learn how to