Sets¶
Sets in Python are an unordered collection of objects used in situations where
membership and uniqueness to the set are the most important information of the
object. The in
operator runs faster with sets than with Lists:
set
¶
Create sets¶
You can create sets by applying set
to a sequence, for example to a
list.
>>> sequences = set(["list", "tuple", "tuple"])
>>> sequences
{'tuple', 'list'}
If a sequence is made into a set, duplicates are removed, but the order is then also lost.
Individual elements cannot be selected with slicing either:
>>> sequences[0]
Traceback (most recent call last):
File "<python-input-27>", line 1, in <module>
sequences[0]
~~~~~~~~~^^^
TypeError: 'set' object is not subscriptable
Check values¶
The keyword in
is used to check whether an object belongs to a set.
>>> "list" in sequences
True
>>> "set" in sequences
False
Add and delete values¶
You can add and delete values with add
and remove
.
>>> quantities = sequences.add("set")
>>> quantities
{'list', 'tuple', 'set'}
>>> quantities.remove("set")
>>> quantities
{'list', 'tuple'}
The elements are unordered, which means that the values within a sequence can shift when new elements are added.
Set formation¶
- Union set
x = {4, 2, 3, 2, 1} y = {3, 4, 5} >>> x.union(y) {0, 1, 2, 3, 4, 5}
- Intersection
>>> x.intersection(y) {3}
Difference or remainder set
>>> x.difference(y) {0, 1, 2}
frozenset
¶
In addition to set
, there is also frozenset
, an immutable data
type. This means that they can also be members of other sets:
1>>> sequences = frozenset(["list", "tuple", "set", "tuple"])
2>>> sequences
3frozenset({'list', 'tuple', 'set'})
4>>> dicts = {"dict"}
5>>> sequences.add(dicts)
6Traceback (most recent call last):
7 File "<python-input-18>", line 1, in <module>
8 sequences.add(dicts)
9 ^^^^^^^^^^^^^
10AttributeError: 'frozenset' object has no attribute 'add'
11>>> dicts.add(sequences)
12>>> dicts
13{frozenset({'list', 'tuple', 'set'}), 'dict'}
Performance¶
Sets are very fast when checking whether elements are contained in a set. The set arithmetic of sets is also well suited to finding common and unique values of two sets. For this purpose, it can be useful to convert Lists or Tuples into sets.
Order¶
However, the speed advantage also comes at a price: sets do not keep the elements in the correct order, whereas Lists and Tuples do. If the order is important to you, you should only convert the elements into a set for certain operations, for example to check whether the elements of a list are unique with
>>> sequences = ["list", "tuple", "set", "tuple"]
>>> len(sequences) == len(set(sequences))
False
Checks¶
How many elements does a set have if it is formed from the following list
[4, 2, 3, 2, 1]
?