Who is in the chess or robotics club?
A set is a collection of different things, such as the students in a club. Set operations tell you who is in one club, in both, or in neither.
A lecturer must book rooms for two clubs, and some students belong to both. Counting them twice books too many rooms. This lesson shows how to combine sets and count their members correctly.
By the end of this lesson
Note
I can combine sets and count their members.
Key words
| Word | What it means |
|---|---|
| Set | a collection of different things, in no order |
| Element | one thing inside a set |
| Union | everything that is in either set |
| Intersection | everything that is in both sets |
| Difference | what is in the first set but not the second |
| Size | how many elements a set has |
The cells students fill in are left blank in this preview.
Union, intersection and difference combine sets
Say the chess club has Ana, Ben, Cai and Dee. The robotics club has Cai, Dee, Eli and Fay.
The union is everyone in either club. The intersection is everyone in both. The difference "chess minus robotics" is everyone in chess who is not in robotics. These match or, and, and "and not" from logic.
Worked example: eight students, two clubs
| Student | Chess | Robotics | Union (either) | Intersection (both) | Chess minus robotics |
|---|---|---|---|---|---|
| Ana | Yes | No | Yes | No | Yes |
| Ben | Yes | No | Yes | No | Yes |
| Cai | Yes | Yes | Yes | Yes | No |
| Dee | Yes | Yes | Yes | Yes | No |
| Eli | No | Yes | Yes | No | No |
| Fay | No | Yes | Yes | No | No |
| Gus | No | No | No | No | No |
| Hal | No | No | No | No | No |
The cells students fill in are left blank in this preview.
Let Python combine the two clubs
python3 -c "c = {'Ana', 'Ben', 'Cai', 'Dee'}; r = {'Cai', 'Dee', 'Eli', 'Fay'}; print(sorted(c | r), sorted(c & r), sorted(c - r))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Add the sizes, then take away the overlap
How many students are in at least one club? Do not add 4 and 4. Cai and Dee are in both lists, so adding counts them twice.
Take the overlap away once: 4 + 4 - 2 = 6. This is called inclusion and exclusion. You include both sets, then exclude the double count.
Worked example: an overlap of 5
| Step | Count |
|---|---|
| Students in chess | 18 |
| Students in robotics | 12 |
| Add them | 30 |
| Students in both, counted twice | 5 |
| Take the overlap away once | 30 - 5 = 25 |
The cells students fill in are left blank in this preview.
Count the union by program
python3 -c "a = set(range(1, 19)); b = set(range(14, 26)); print(len(a), len(b), len(a & b), len(a | b))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think the size of a union is the two sizes added. That is true only when the sets share nothing. When they overlap, the shared elements are counted twice, so take the size of the intersection away once.
A set ignores order and repeats
A list can hold the same value twice and keeps its order. A set cannot and does not. The set {3, 1, 3, 2} is just {1, 2, 3}.
A set A is a subset of B when every element of A is also in B.
Turn a list into a set
python3 -c "print(sorted(set([3, 1, 3, 2, 1])), {1, 2} <= {1, 2, 3})"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Try it: find the overlap with a loop
chess = [1, 2, 3, 4, 5, 6]
robotics = [5, 6, 7, 8]
both = []
for name in chess:
if name in robotics:
both.append(name)
print("in both:", both)
print("in either:", len(chess) + len(robotics) - len(both))
# Add 9 to robotics and press Run again. What changes?