Sets#
(Click here for the German version of this page)
A set is an unordered collection of elements, where no duplicates are allowed.
One can work with sets in Python just like one is used to from mathematical sets.
Since it is an unordered collection of elements, one should not assume that e.g., the set stores elements in the same order as you have added them. For those purposes, a list or something else should be used instead.
Additionally, elements in a set, in contrast to lists, must be immutable. So, elements of a set have the same restriction and keys in a dictionary.
Syntax#
The set syntax is very similar to lists. The only difference is that instead of using square brackets, curly braces are used. Otherwise, the creation is identical.
my_list = [1, 2, 3]
my_set = {1, 2, 3}
Since the same delimiters ({
und }
) are also used for dictionaries, it is not entirely clear what {}
should be interpreted as. It could either be an empty set or an empty dictionary. Python defines {}
to be an empty dictionary. To create an empty set, one should instead use the function set()
.
print(type( {} ))
# --> <class 'dict'>
print(type( set() ))
# --> <class 'set'>
Adding and deleting elements in a set#
To add an element to a set, the function Set.add(Element)
is used. If one uses Set.add(Element)
to try and add an element, which is already present, the set will not be altered.
my_set = {1, 2, 3}
my_set.add(5)
my_set.add(4)
my_set.add(4)
print(my_set)
# --> {1, 2, 3, 4, 5}
# Observe, that 5 was inserted before 4,
# but the output lists 4 before 5.
# Since a set is an unordered collection,
# the order of elements is not guaranteed.
With Set.remove(Element)
one can remove an element from a set.
my_set = {1, 2, 3}
my_set.remove(3)
print(my_set)
# --> {1, 2}
Checking whether an Element is contained in a set#
The keyword in
can be used to check whether an element is contained in a set.
planets = {"Earth", "Saturn", "Mars"}
print( "Saturn" in planets )
# --> True
print( "Pluto" in planets )
# --> False
Set operations#
The interesting part of sets are set operations. These include e.g., union, intersection, difference and symmetric difference.
The union \(A \cup B\) can be calculated using the method A.union(B)
or A | B
for short.
The intersection \(A \cap B\) can be calculated using the method A.intersection(B)
or A & B
for short.
The difference \(A \setminus B\) can be calculated using the method A.difference(B)
or A - B
for short.
Die symmetric difference \(A \triangle B\) can be calculated using the method A.symmetric_difference(B)
or A ^ B
for short.
a = {1, 2, 3}
b = {3, 4, 5}
print( a.union(b) ) # a | b
# --> {1, 2, 3, 4, 5}
print( a.intersection(b) ) # a & b
# --> {3}
print( a.difference(b) ) # a - b
# --> {1, 2}
print( a.symmetric_difference(b) ) # a ^ b
# --> {1, 2, 4, 5}
Exercise#
Use set operations to implement the following expression.
# Here you can try to solve this problem...
A = {1, 2, 3}
B = {6, 7, 1}
C = {6}
D = {6, 7, 3}
result = ...
print(result)
Ellipsis
Solution
A = {1, 2, 3}
B = {6, 7, 1}
C = {6}
D = {6, 7, 3}
result = A.union(B).intersection(B).difference(D.symmetric_difference(C))
print(result)
# --> {1, 6}
# Or the short form:
result = ((A | B) & B) - (D ^ C)
print(result)
# --> {1, 6}
The result should be the set {1, 6}
.