How to Delete or Remove Element From Set in Python

Last Updated on June 15, 2021 by Roshan Parihar

In this tutorial, learn how to delete or remove element from Set using Python. The short answer is to use the remove() function and discard() function of Python and pass Set element as its argument to remove.

You can pass a single element to remove from the Set in Python. However, to delete multiple elements, you have to use the for loop of Python.

Let’s find out with the examples given below here to learn the methods.

Method 1: Using Remove() to Remove Element From Set in Python

To remove single or multiple elements from a Set, you can use the remove() as given below.

Remove Single Element

When you want to remove the single element from a Set, you have to specify a single element as the argument of the remove() function.

Output

{13.2, ‘Shyam’, ‘Ram’, ‘Feroz’, ‘Bilal’}

The above example uses the function to remove element ’10’ from the Set in Python. The output shows that the specified element is removed from the Set variable.

Remove Multiple Elements

You can use the same remove() to remove multiple elements from Set in Python. However, you have to use a for loop to remove more than one element in each iteration. Store all the elements in Python List that you want to remove from Set as given below.

Output

{13.2, ‘Shyam’, ‘Bilal’, ‘Ram’}

The output shows that elements ’10’ and ‘Feroz’ are removed from the Set variable. The example removes two elements and you can add more elements to the List variable to remove from Set.

Method 2: Using Discard() to Delete Items From Set in Python

The discard() is also useful to delete items from Set in Python. It works the same as the function given in the above example. See the examples given below to learn the method.

Delete Single Element

To delete a single element from Set in Python, you have to use the discard() function and pass the element as its argument. The below example specifies the element ’10’ to delete from the Set variable.

Output

{‘Bilal’, 13.2, ‘Shyam’, ‘Feroz’, ‘Ram’}

The output shows that the function deleted the specified element from the Set in Python.

Delete Multiple Elements

You can also use the discard() to delete multiple items from Set in Python. However, you have to first place all the elements in Python List that you want to delete from the Set. After that, it requires to use the for loop to delete more than one element on each iteration of the loop.

Output

{13.2, ‘Shyam’, ‘Bilal’, ‘Ram’}

The above example shows that output with four elements in a Set after elements ’10’ and ‘Feroz’ get deleted.

Method 3: Using Pop() Eliminate Random Items

In addition to the above examples, you can also use the pop() to eliminate a single element from the Set. However, the function removes the random element from the Set. Let’s see the example given below.

Output

{‘Feroz’, 10, ‘Bilal’, 13.2, ‘Ram’}

The above example removes the single element from the Set. However, you cannot specify which one the function has to remove from the Set.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.