Sets
After this: Keep each value once, ask whether a value has been seen, and pick the right shape for a job.
On this page · 8 sections · 3 checks
At the door of an event, every ticket is scanned. For each ticket there is one question: has this number been scanned before? There is nothing to look up and no count to keep, only the question. A list could hold the numbers scanned so far, but it would keep every repeat, and to answer the question it would have to be searched from the start each time.
Each value once
A is a collection that keeps each value once. Adding a value that is already there changes nothing. A set has no positions: you cannot ask for "the second value", only "is this value in it?". Think of it as the keys of a dictionary with no values attached.
By hand
Start with an empty set. Add 4, then 2, then 4 again, then 7.
- 4 is new. One value.
- 2 is new. Two values.
- 4 is already there. Nothing changes.
- 7 is new. Three values.
Four adds, three values: 2, 4 and 7.
In code
Make an empty set with set()new HashSet<>(), add a value with add, and count the values with len(seen)seen.size(). Step through the four adds and watch the box on the third one.
- Run the programRun it and watch the box seen. Four values are added, but one of them is already there. Which line leaves the box as it was?
seen = set()seen.add(4)seen.add(2)seen.add(4)seen.add(7)print(len(seen))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Is it in there?
The question a set exists for: 4 in seenseen.contains(4) is Truetrue when 4 is one of the values and Falsefalse when it is not.
seen = {2, 4, 7}print(4 in seen)print(5 in seen)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Put the question inside a loop and you have the door scanner. For each ticket, ask whether it is in the set, and then add it.
Change the tickets so that two different numbers repeat, and run again.
Dropping repeats from a list
Turning a list into a set drops the repeats, so the size of the set is the number of different values in the list.
numbers = [1, 2, 2, 3, 3, 3]unique = set(numbers)print(len(unique))print(unique)3
{1, 2, 3}ArrayList<Integer> numbers = new ArrayList<>(List.of(1, 2, 2, 3, 3, 3));HashSet<Integer> unique = new HashSet<>(numbers);System.out.println(unique.size());System.out.println(unique);3 [1, 2, 3]
Which shape
| You need to | Use |
|---|---|
| keep values in order and read them by position | a list |
| look a value up by a key | a dictionary |
| know whether a value has been seen, with no repeats | a set |
Ask what you find things by. A position: a list. A key: a dictionary. Only "is it there": a set.
Check yourself
The list [1, 2, 2, 3, 3, 3] is turned into a set. How many values does the set hold?
Two adds, then the size and a question. What does this print?
seen = {2, 4, 7}seen.add(4)seen.add(9)print(len(seen))print(5 in seen)HashSet<Integer> seen = new HashSet<>(Set.of(2, 4, 7));seen.add(4);seen.add(9);System.out.println(seen.size());System.out.println(seen.contains(5));You have every student's name and their mark, and the program must find a student's mark when given the name. Which shape fits best?
Honesty notes
- Say what a set is: a collection that keeps each value once and has no positions.
- Add a value to a set and ask whether a value is in it (in for Python, contains in Java).
- Turn a list into a set to drop the repeats, and count what is left.
- Choose a list, a dictionary or a set by asking what you need to find things by.