Sets are a collection of unordered items, meaning that you cannot access the elements of a set using indexes. Therefore, deleting an item from the set becomes confusing for new programmers to Python. However, there are predefined methods that the user can utilize to delete the required element from the set in Python.
This post will cover these methods in detail and will contain the following content:
- Method 1: Use the remove() Method to Remove an Element From Set
- Method 2: Use the discard() Method to Remove an Element From Set
- Method 3: Use the Subtraction Operator to Remove Multiple Elements From the Set
- Method 4: Using the difference() Method to Remove Multiple Elements From the Set
Method 1: Use the remove() Method to Remove an Element From Set
The remove() method can be used to delete one element from a set in Python by applying it to a set variable through dot operator, and it takes the value to be deleted from the set as an argument. To demonstrate the working of the remove() method, take the following code snippet:
print("Initial Set: ",setvar)
setvar.remove(16)
print("Updated Set: ",setvar)
When this code is executed, it will produce the following output on the terminal:
From the output, you can easily confirm that the value “16” has been successfully removed from the set.
Method 2: Use the discard() Method to Remove an Element From Set
Another function that can be used to remove an element from the set is the discard() method, and similar to the remove() method, it can be applied to the set variable by using a dot operator. It also takes the value to be removed from the set in its argument. To demonstrate the working of the discard() method, take the following code snippet:
print("Using the discard() method")
print("Initial Set: ",setvar)
setvar.discard(99)
print("Updated Set: ",setvar)
When this code is executed, it produces the following output on the terminal:
The output verifies that the value “99” has been successfully removed from the set.
Method 3: Use the Subtraction Operator to Remove Multiple Elements From the Set
You can also use the subtraction operator “–” to remove multiple elements from a Set. However, to use this method, you must create another set containing the values you want to delete from the original set.
To demonstrate this method of using the subtraction operator, take the following code snippet:
print("Using the Subtraction Operator Method")
print("Initial Set: ",setvar)
delVar ={35,12,75}
setvar = setvar - delVar
print("Updated Set: ",setvar)
When this code is executed, it produces the following output on the terminal:
As you can see from the output image above, the values “32, 17, and 75” were removed from the set using the subtraction operator.
Method 4: Using the difference() Method to Remove Multiple Elements From the Set
The differences() method can also be used to remove multiple elements from a given set. This method is applied to the set variable by using a dot operator and takes in a set of values to remove from the original set.
To demonstrate the working of the difference() method to remove elements from the set, take the following code snippet:
print("Using the difference() Method")
print("Initial Set: ",setvar)
delVar ={35,12,99,36}
setvar = setvar.difference(delVar)
print("Updated Set: ",setvar)
When this code is executed, it produces the following outcome on the terminal:
From the output, it can be observed that the elements “35, 12, 99, and 36” were removed from the set.
Conclusion
Multiple different methods can be used to remove single or multiple elements from a set in Python. The user can use the remove() method and the discard() method to remove a single element/item from a Python Set. If the user’s requirement is to remove multiple elements from a set, then the user can use the difference() method or the subtraction operator “-” approach.