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
1 2 3 4 5 6 7 8 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #Remove a 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()
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
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 an 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.
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
1 2 3 4 5 6 7 8 |
#declare Set in Python mySet = {"Ram", "Shyam", 10, "Bilal", 13.2, "Feroz"} #Delete a 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.
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
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 an 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.
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
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