How to Remove an Element From a Set in Python

Last Updated on May 11, 2024 by Roshan Parihar

To remove an element from a Set in Python, use the remove() function and discard() function and pass the Set element as its argument.

You can also use the for loop to remove multiple elements from a Set. Let’s find out with the examples.

Remove Element From Set Using Remove() Function in Python

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

Remove Single Element

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

Example 1

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() function 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.

Example 2

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.

Using Discard() Function to Remove Element 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.

Remove Single Element Using discard()

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.

Example 3

Output

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

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

Remove Multiple Elements Using discard() Function

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 using the for loop to delete more than one element on each iteration of the loop.

Example 4

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.

Using Pop() Eliminate Random Elements

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.

Example 5

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.