P04: Sets¶
Sets¶
Sets are unordered, mutable, collections of distinct elements.
In Python sets are like lists in that they:
are mutable (so you can change them),
are collections of other objects (so you can iterate over them, get their
len()
, check membership within
).
However, they are unlike lists in that:
They are unordered so they cannot be indexed or sliced.
They cannot contain things like lists, dictionaries, or other sets.
Every item they contain must be distinct.
Sets are created with set(vals)
where vals
is typically a list or some other kind of iterable entity.¶
x = set(['a', 'b', 3, 4])
print(x)
{'a', 3, 4, 'b'}
Sets can be modified with the methods .add()
or .remove()
¶
print(x)
x.add('abs')
x.add(10)
print(x)
x.add(3) # no effect
print(x)
{'a', 3, 4, 'b'}
{3, 4, 10, 'a', 'b', 'abs'}
{3, 4, 10, 'a', 'b', 'abs'}
Note: sets are presented in some kind of order, but that order changes when the set is changed.¶
Adding an existing item has no effect, since sets have only disctinct items.
print(x)
x.remove(4) # remove the value 4 from the set
print(x)
{3, 4, 10, 'a', 'b', 'abs'}
{3, 10, 'a', 'b', 'abs'}
Set membership can be evaluated with in
¶
6 in x
False
Sets can be iterated over with a for loop
¶
for item in x:
print(item)
3
10
a
b
abs
Set operations¶
Sets are useful because:
they are efficient for keeping track of unique things.
they support set operations (union, intersection, difference)
A = set('panda')
B = set('conga')
print(A)
print(B)
{'d', 'a', 'n', 'p'}
{'o', 'c', 'g', 'n', 'a'}
Set union: items in A or B¶
print(A | B)
{'p', 'o', 'c', 'g', 'd', 'a', 'n'}
Set intersections: items in A and B¶
print(A & B)
{'a', 'n'}
Set difference: items in A but not B¶
print(A - B)
{'d', 'p'}
Set symmetric difference: items in either set but not both sets (i.e. items in union but not in intersection)¶
print(A ^ B)
{'d', 'o', 'c', 'g', 'p'}