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.
1 2 3 4 5 6 7 8 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #Remove single element from a Set using remove() mySet.remove(10) #Print result print(mySet) |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #List of multiple Elements to remove from Set toRemove = [10, "Feroz"] #Remove multiple elements from a Set using remove() for item in toRemove: mySet.remove(item) #Print result print(mySet) |
Output
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.
1 2 3 4 5 6 7 8 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #Delete single element from a Set using discard() mySet.discard(10) #Print result print(mySet) |
Output
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #List of multiple elements to delete from Set toRemove = [10, "Feroz"] #Delete multiple elements from a Set using discard() for item in toRemove: mySet.discard(item) #Print result print(mySet) |
Output
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.
1 2 3 4 5 6 7 8 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #Eliminate random items from Set using pop() mySet.pop() #Print result print(mySet) |
Output
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