After this lesson you can remove duplicates from data, ask quickly whether something is present, and compare two groups of items with set operations. A set is a collection of unique values, much like the sets you may remember from maths.
Creating a set
Write a set with curly braces. Python quietly throws away repeats:
colours = {"red", "green", "blue", "red", "green"}
print(len(colours))
print(sorted(colours))3
['blue', 'green', 'red']We print sorted(colours) rather than colours on purpose. A set does not promise any particular order, and the order you see can differ between runs, so you should never depend on it. When you need to show a set in a stable way, sort it, which gives you a list.
For small whole numbers, the order tends to look sorted, but that is an implementation detail and not something to rely on:
numbers = {3, 1, 2, 3, 1}
print(numbers)
print(len(numbers)){1, 2, 3}
3To make an empty set, call set(). Writing {} makes an empty dictionary, which is a different type that you will meet shortly.
empty_set = set()
empty_dict = {}
print(type(empty_set))
print(type(empty_dict))<class 'set'>
<class 'dict'>set() also converts other collections. That is the quickest way to remove duplicates:
visits = ["Asha", "Ben", "Asha", "Chloe", "Ben", "Asha"]
unique_visitors = set(visits)
print(len(visits), len(unique_visitors))
print(sorted(unique_visitors))6 3
['Asha', 'Ben', 'Chloe']If you also need the original order kept, a set alone will not do; the dictionary lesson shows a trick for that.
No indexes, but fast membership
Because a set has no order, there is no position to ask for:
colours[0]TypeError: 'set' object is not subscriptableWhat a set is very good at is answering "is this in here?". Testing with in is fast even when the set is large, much faster than scanning a long list:
allowed = {"admin", "editor", "viewer"}
print("editor" in allowed)
print("guest" in allowed)
print("guest" not in allowed)True
False
TrueYou can still loop over a set with for, but remember the order is not defined. Items that go into a set must be hashable, which in practice means immutable values such as numbers, strings and tuples. A list cannot be a set member:
bad = {[1, 2], [3, 4]}TypeError: unhashable type: 'list'Adding and removing
Sets are mutable. Add one item with add, many with update:
tags = {"python", "sql"}
tags.add("git")
tags.add("python")
tags.update(["css", "html"])
print(sorted(tags))['css', 'git', 'html', 'python', 'sql']To remove, remove raises a KeyError if the item is missing, while discard stays quiet:
tags.remove("css")
tags.discard("not-there")
print(sorted(tags))['git', 'html', 'python', 'sql']Comparing sets
The real power of sets is comparing groups. Imagine two classes and the students who joined each:
maths = {"Asha", "Ben", "Chloe", "Daniel"}
art = {"Chloe", "Daniel", "Elena"}
print(sorted(maths | art))
print(sorted(maths & art))
print(sorted(maths - art))
print(sorted(art - maths))
print(sorted(maths ^ art))['Asha', 'Ben', 'Chloe', 'Daniel', 'Elena']
['Chloe', 'Daniel']
['Asha', 'Ben']
['Elena']
['Asha', 'Ben', 'Elena']Read them as follows. | is the union, everyone in either class. & is the intersection, only students in both. - is the difference, the first set without anything from the second, so order matters. ^ is the symmetric difference, those in exactly one of the two classes.
Each operator also has a method form, such as maths.union(art) and maths.intersection(art). The methods accept any collection, not just sets. Testing relationships is just as easy:
small = {1, 2}
big = {1, 2, 3}
print(small <= big)
print(big >= small)
print(small.isdisjoint({7, 8}))True
True
True<= asks whether every item of the left set is in the right one, and isdisjoint asks whether the two share nothing.
Common mistakes
- Using
{}for an empty set. It is a dictionary. Writeset(). - Relying on the printed order. It is not guaranteed. Use
sorted()whenever the order matters to you. - Indexing or slicing a set. There are no positions. Convert to a list or sorted list first if you need one.
- Putting lists or other mutable items in a set. Use tuples instead.
- Mixing up
removeanddiscard.removefails when the item is missing;discarddoes not.
Exercise
Two shops stock these items: shop A has "pen", "ruler", "glue", "paper" and shop B has "paper", "tape", "pen". Print, in sorted order, the items both shops sell, the items only shop A sells, and every item sold by either shop.
Show one possible answer
shop_a = {"pen", "ruler", "glue", "paper"}
shop_b = {"paper", "tape", "pen"}
print(sorted(shop_a & shop_b))
print(sorted(shop_a - shop_b))
print(sorted(shop_a | shop_b))['paper', 'pen']
['glue', 'ruler']
['glue', 'paper', 'pen', 'ruler', 'tape']Previous: Tuples. Next: Dictionaries.
Key points
- A set holds unique items with no duplicates. Build one with curly braces, or with set() for an empty one, since {} is an empty dictionary.
- The order of a set is not guaranteed and sets cannot be indexed. Use sorted(my_set) when you need a stable order.
- Membership tests with in are very fast on sets, which makes them a good fit for lookups.
- Union (|), intersection (&), difference (-) and symmetric difference (^) answer questions such as who is in both groups.