Remove items from Set
One of the property of the set is that it is unchangeable. But, we can still add or remove items from the set. Python has some built in methods to remove the items from the set. We will learn different approaches and methods we can use to delete the items from the set.
The remove() method
The remove() is a built-in method to remove the specific item from the set. The element to remove is passed as an argument to the set. It can accept only one element as an argument, means i can delete only one element at a time.
It will raise an error if the item does not exist in the set. To avoid the error, we can use the membership operator “in” to check if the item is available in the set.
names = {"Bill", "Steve", "Tim"}
names.remove("Steve")
print(names)
Raises an error if item does not exist.
names = {"Bill", "Steve", "Tim"}
names.remove("Oliver")
print(names)
Use membership operator to avoid error.
names = {"Bill", "Steve", "Tim"}
if ("Oliver" in names):
names.remove("Oliver")
print(names)
The discard() method
The discard() method is also used to remove the specific item from the set. It is similar to the remove() method, the only difference is that it will not raise an error if the item does not exist.
names = {"Bill", "Steve", "Tim"}
names.discard("Steve")
print(names)
Do not raise an error if item does not exist.
names = {"Bill", "Steve", "Tim"}
names.discard("Oliver")
print(names)
Delete multiple items using remove() and discard()
To delete multiple items with the remove() or discard() method, we can use this method with the for loop. For loop will iterate over items one by one and these methods will remove the items.
The remove() method
names = {"Bill", "Steve", "Tim"}
names_to_remove = ["Bill", "Steve"]
for name in names_to_remove:
names.remove(name)
print(names)
The discard() method
names = {"Bill", "Steve", "Tim"}
names_to_remove = ["Bill", "Steve"]
for name in names_to_remove:
names.discard(name)
print(names)
The pop() method
The pop() method will remove the random item from the set and return the removed item. In case the set is empty, the KeyError is raised.
names = {"Bill", "Steve", "Tim"}
item_removed = names.pop()
print(item_removed)
print(names)
Using pop with Empty Set.
names = set()
item_removed = names.pop()
print(item_removed)
print(names)
The difference() or difference_update() method
The difference() and difference_update() allows us to remove multiple items from the set. While using these methods, we need not to use the for loop to iterate over the elements. We just need to pass the iterable as the set or list, etc. All those elements will be removed from the original set.
The difference() method will return the new set but the differeence_update() method will update the original set.
The difference() method
names = {"Bill", "Steve", "Tim"}
names_to_remove = ["Bill", "Steve"]
names = names.difference(names_to_remove)
print(names)
The difference_update() method
names = {"Bill", "Steve", "Tim"}
names_to_remove = {"Bill", "Steve"}
names.difference_update(names_to_remove)
print(names)
Using the – Operator
The – operator works in the same way as the difference() method, the only difference is that, it will work with sets only and the difference() method can work with the other iterables like list, etc.
The – operator works with set only.
names = {"Bill", "Steve", "Tim"}
names_to_remove = {"Bill", "Steve"}
names = names - names_to_remove
print(names)
The – operator will not work with list.
names = {"Bill", "Steve", "Tim"}
names_to_remove = ["Bill", "Steve"]
names = names - names_to_remove
print(names)
The del keyword
The del keyword is used to delete the set completely.
Deletes the set completely.
names = {"Bill", "Steve", "Tim"}
del names
print(names)
The clear() method
The clear() method removes all the items from the set. The set variable still exists with no content.
names = {"Bill", "Steve", "Tim"}
names.clear()
print(names)
Conditionally remove items from the set
To conditionally remove items from the set, we have to use the if condition for the items that we want to remove. Note: For this approach to work, we need to make sure, set is converted to list for the loop to work. Otherwise, it will throw an error.
Conditionally remove items from the set by converting set to list for loop.
names = {"Bill", "Steve", "Tim"}
for name in list(names):
if name.startswith("B"):
names.remove(name)
print(names)
Error in case we don’t convert set to list for loop.
names = {"Bill", "Steve", "Tim"}
for name in names:
if name.startswith("B"):
names.remove(name)
print(names)
Quick Recap
Here is a quick recap of the different approaches we can use to remove items from a set.
Approach | Detail |
---|---|
The remove() method | Remove a specific item. |
The discard() method | Removes a specific item without error. |
Delete multiple items using remove() and discard() | The for loop is used. |
The pop() method | Removes and returns random item. |
The difference() or difference_update() method | Remove Multiple Items. |
The del keyword | Deletes set completely. |
The clear() method | Empty the set. |
Conditionally remove items from the set | Using for and if statement |