In one of my previous articles we were talking about the basics of sets. Today we’ll be talking about set methods. We’ll learn how to add elements to a set, remove them, clear the whole set and copy it. So, let’s get down to work.
You can also watch the video version of this article:
Table of Contents
Adding Elements to a Set
Sets are mutable, so we can add elements to them. We can only add immutable elements. We use the add method to add elements:
>>> trees = set(["oak", "poplar", "birch"])
>>> trees.add("beech")
>>> trees
{'poplar', 'birch', 'oak', 'beech'}
If we try to add a mutable element (like a list), we get an error:
>>> trees.add(["spruce", "maple"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
If we try to add an element which already is in the set, it will have no effect:
>>> trees.add("poplar")
>>> trees
{'poplar', 'birch', 'oak', 'beech'}
Set Methods Used to Remove Elements from Sets
We can also remove elements. There are a couple set methods that come in handy. One of them is the method discard:
>>> trees = {'poplar', 'birch', 'oak', 'beech'}
>>> trees.discard("oak")
>>> trees
{'poplar', 'birch', 'beech'}
If the element we’re trying to remove is not found in the set, nothing happens:
>>> food = {"apple", "bread", "egg"}
>>> food.discard("butter")
>>> food
{'apple', 'egg', 'bread'}
There is another method, remove, that works pretty much the same as discard. The difference is that the remove method raises an error if the element is not found in the set:
>>> food = {"apple", "bread", "egg"}
>>> food.remove("butter")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'butter'
There’s also the pop method. The pop method returns and removes an arbitrary element of the set:
>>> sports = {"tennis", "football", "baseball", "soccer"}
>>> sports.pop()
'soccer'
>>> sports
{'tennis', 'baseball', 'football'}
>>> sports.pop()
'tennis'
>>> sports.pop()
'baseball'
>>> sports.pop()
'football'
>>> sports
set()
If you use pop on an empty set, an error is raised:
>>> sports.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'
It’s also possible to clear a set. To this end we need the method clear. We get an empty set to which we can later add elements:
>>> months = {"July", "May", "December"}
>>> months.clear()
>>> months
set()
>>> months.add("November")
>>> months
{'November'}
Set Methods in Frozensets
If we need an immutable set, we should use a frozenset. Frozensets don’t have the methods add and discard:
>>> trees = frozenset(["oak", "poplar", "birch"])
>>> trees.add("beech")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> trees.discard("oak")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'discard'
Copying Sets
We use the method copy to create a shallow copy. The method returns the copy.
>>> furniture = {"chair", "sofa", "table", "bed"}
>>> furniture_copy = furniture.copy()
Let’s remove an element from the original:
>>> furniture.discard("sofa")
and let’s compare the original to the copy. The element removed from the original is still in the copy, so it works.
>>> furniture
{'bed', 'chair', 'table'}
>>> furniture_copy
{'bed', 'chair', 'sofa', 'table'}