General Tips and Traps¶
The set class is implemented based on hash table which means that its elements must be hashable (has methods
__hash__
and__eq__
). The set class implements the mathematical concepts of set which means that its elements are unordered and does not perserve insertion order of elements. Notice that this is different from the dict class which is also implemented based on hash table but keeps insertion order of elements! The article Why don't Python sets preserve insertion order? has a good explanation of the rational behind this.The 3rd-party Python library sortedcontainers has implementations of sorted containers. Specially, there is a class named
SortedSet
.
Construct a Set¶
set
accepts a single iterable argument instead of varargs!set("abc")
creates set with elements"a"
,"b"
and"c"
instead of a set with a single element"abc"
!set(1, 2, 3)
won't create a set of elements1
,2
and3
but will instead throw TypeErro as set requires a single iterable argument.
set("abc")
set(1, 2, 3)
Create a set containing numbers 0-9 using range
.
set(range(0, 10))
Or you use set comprehension.
s = set(i for i in range(0, 10))
s
set.add¶
set.add
adds an Element in place.
s = set([1, 2, 3])
s.add(4)
s
set.union¶
s1.union(s2)
returns a new set containing the union of s1
and s2
(an iterable)
and s1
is unchanged.
Note: there is no +
operator (use set.union
instead) for sets even if there is -
operator for sets.
s = set([1, 2, 3])
s.union([2, 3, 4])
s
You can union multiple iterables (of elements) at the same time.
s.union([2, 3, 4], [3, 4, 5])
arrs = [[2, 3, 4], [3, 4, 5]]
arrs
s.union(*arrs)
set.update¶
set.update
inserts all elements of another iterable in place.
You can think of set.update
as the mutable version of set.union
.
s = set([1, 2, 3])
s.update([2, 3, 4])
s
set.difference¶
s1.difference(s2)
returns a new set containing the difference between s1
and s2
(an iterable)
and s1
is unchanged.
When s1
and s2
are both sets,
s1.difference(s2)
is equivalent to the -
operator.
To sum up,
set.difference
is more flexible than the operator -
and is preferred.
Note: there is no +
(use set.union
instead) operator for sets.
s = set([1, 2, 3])
s
s.difference([2, 3, 4])
s
s - set([2, 3, 4])
set.pop¶
s = set(range(10))
s
s.pop()
s
set.intersection¶
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63]
c2 = [13, 17, 18, 21, 32]
c3 = [13, 59, 67]
set(c1).intersection(c2)
set(c1).intersection(c2, c3)
set(c1).intersection(*[c2, c3])