Set type
Python also offers a set
data structure.
Like in Mathematics, a set is an unordered collection of unique elements.
So, a set should not have duplicate elements, nor should the ordering be important.
Like list
s and tuple
s, elements in a set
do not have to be of the same type (but again, think carefully about whether a set is really the most suitable data structure in such a case).
A set
is presented with curly braces {}
in Python.
>>> vowels = {"a", "e", "i", "o", "u"}
>>> print(vowels) # note the ordering
>>> vowels = {"i", "o", "a", "a", "e", "u", "e", "i", "a"}
>>> print(vowels)
You can also convert another sequence (e.g. list
or tuple
) into a set using the set()
constructor. Here is one practical application for this:
>>> words = ["let", "it", "go", ",", "let", "it", "go", ",", "can't",
... "hold", "it", "back", "any", "more", "!"]
>>> vocabulary = set(words)
>>> print(vocabulary)
To create an empty set, you should use the set()
constructor. You cannot use {}
because you will end up with a dict
which also happens to use {}
(we will discuss dict
s after this).
>>> empty_set = set()
>>> print(empty_set)
>>> print(type(empty_set))
>>> empty_dict = {}
>>> print(type(empty_dict))
The elements in a set must be immutable. Why? Hint: how would a set keep track of unique elements otherwise?
>>> set([[1,2], [1,2], [1,3]]) # NO! Look at the error message.
>>> set([(1,2), (1,2), (1,3)]) # This is ok.
You cannot access an element in a set. Why? Hint: Think about the definition of set.
>>> x = {1, 2, 3}
>>> print(x[1]) # NO!
You will need to convert a set to a list if you need to access individual elements.
>>> x = {1, 2, 3}
>>> y = list(x)
>>> print(y[1])
Adding elements to a set
You can use set
’s add()
method to add a new element to the set.
>>> x = {1, 3, 2}
>>> x.add(4)
>>> print(x)
You can also add multiple elements in one go with the update()
method. You can update a set with any type of sequence (list
, tuple
).
>>> x = {8, 2, 5}
>>> x.update([3, 5, 2, 3])
>>> print(x)
Removing elements from a set
You can remove an element from a set with the discard()
method. Python will not do anything if you try to remove an element that is not in the set.
>>> x = {3, 9, 7}
>>> x.discard(3)
>>> print(x)
>>> x.discard(8)
>>> print(x)
A stricter method called remove()
will cause Python to raise an error if you try to remove an element that is not in the set. May be useful if you need such a behaviour!
>>> x = {3, 9, 7}
>>> x.remove(8) # What is the error message?
Set operations
Python provides overloaded operators for various set operations.
Operation | Math Notation | Python Operator |
---|---|---|
Union | \(A \cup B\) | set_a | set_b |
Intersection | \(A \cap B\) | set_a & set_b |
Difference | \(A \setminus B\) | set_a - set_b |
Symmetric difference | \(A \triangle B\) | set_a ^ set_b |
Membership | \(x \in B\) | x in set_b |
Non-membership | \(x \notin B\) | x not in set_b |
Proper Subset | \(A \subset B\) | set_a < set_b |
Subset | \(A \subseteq B\) | set_a <= set_b |
Proper Superset | \(A \supset B\) | set_a > set_b |
Superset | \(A \supseteq B\) | set_a >= set_b |
Play around with these in your Python interpreter, and make sure that they do what you expected them to do. Also think about what the output of the operator is: Is it a boolean expression? Or a set?