Sets¶
Sets in Python are an unordered collection of objects that are 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
¶
1 >>> x = set([1, 2, 3, 2, 4])
2 >>> x
3 {1, 2, 3, 4}
4 >>> 1 in x
5 True
6 >>> 5 in x
7 False
8 >>> x.add(0)
9 >>> x
10 {0, 1, 2, 3, 4}
11 >>> x.remove(4)
12 >>> x
13 {0, 1, 2, 3}
14 >>> y = set([3, 4, 5])
15 >>> x | y
16 {0, 1, 2, 3, 4, 5}
17 >>> x & y
18 {3}
19 >>> x ^ y
20 {0, 1, 2, 4, 5}
21 >>> x.update(y)
22 >>> x
23 {0, 1, 2, 3, 4, 5}
- Line 1
You can create a set by applying
set
to a sequence, for example to a list.- Line 3
When a sequence is made into a set, duplicates are removed.
- Lines 4–7
The keyword
in
is used to check whether an object belongs to a set.- Lines 8–13
With
add
andremove
you can change the elements in set.- Line 15
|
is used to get the union or combination of two sets.- Line 17
&
is used to get the intersection.- Line 19
^
is used to find the symmetrical difference, meaning elements that are contained in one or the other set, but not in both.
frozenset
¶
In addition to set
, there is also frozenset
, which is immutable. This
means that they can also be members of other sets:
1>>> x = set([4, 2, 3, 2, 1])
2>>> z = frozenset(x)
3>>> z
4frozenset({1, 2, 3, 4})
5>>> z.add(5)
6Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8AttributeError: 'frozenset' object has no attribute 'add'
9>>> x.add(z)
10>>> x
11{1, 2, 3, 4, frozenset({1, 2, 3, 4})}
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 use a data structure that remembers the order.
Checks¶
How many elements does a set have if it is formed from the following list
[4, 2, 3, 2, 1]
?