Unit 10: Sets & Pairs
No repeats, and looping two at a time.
Unit 10 of 31 in Python for kids. Its 5 lessons are No Repeats Allowed, Comparing Sets, Counting As You Go, Two Lists at Once and Set Master — below is everything each one explains, and a question or two from it to try.
Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🎯 No Repeats Allowed
A bag that refuses duplicates
A set uses curly brackets and can only ever hold one of each thing. Put the same value in twice and the second one is simply ignored.
Python
pets = {"cat", "dog", "cat"}
print(len(pets))
It prints
2
Sets have no order
A list remembers the order you built it in. A set does not — it keeps things wherever it likes. So sort it before printing whenever the order matters to you.
Python
nums = {3, 1, 2}
print(sorted(nums))
It prints
[1, 2, 3]
The tidy way to remove duplicates
Wrap a list in set() and every repeat disappears. Wrap that in sorted() and you get a clean list back.
Python
scores = [5, 3, 5, 1, 3]
print(sorted(set(scores)))
It prints
[1, 3, 5]
Adding and taking away
add puts something in, discard takes it out — and discard stays calm if it was never there in the first place.
Python
pets = {"cat"}
pets.add("dog")
pets.add("cat")
pets.discard("fish")
print(sorted(pets))
It prints
['cat', 'dog']
Try it yourself
What is the main difference between a list and a set?
- A set is bigger
- A set keeps no duplicates and no order
- A set can only hold numbers
- A set cannot be looped over
What does this print?
Python
letters = set("banana")
print(len(letters))
Answer them in the app
🔗 Comparing Sets
Everything in either one
| is union — everything that is in one set or the other, still with no repeats.
Python
mine = {"cat", "dog"}
yours = {"dog", "fish"}
print(sorted(mine | yours))
It prints
['cat', 'dog', 'fish']
Only what you both have
& is intersection — only the things in both. Perfect for "which snacks do we both like?"
Python
mine = {"cat", "dog"}
yours = {"dog", "fish"}
print(sorted(mine & yours))
It prints
['dog']
What only you have
- is difference — what is in the first set and not the second. Swapping the sides gives a different answer.
Python
mine = {"cat", "dog"}
yours = {"dog", "fish"}
print(sorted(mine - yours))
print(sorted(yours - mine))
It prints
['cat'] ['fish']
Try it yourself
What does this print?
Python
a = {1, 2, 3}
b = {3, 4}
print(sorted(a & b))
print(sorted(a | b))
Answer it in the app
🔢 Counting As You Go
The position and the thing, together
Looping over a list gives you each item, but not where it sat. enumerate hands you both at once.
Python
pets = ["cat", "dog"]
for i, pet in enumerate(pets):
print(i, pet)
It prints
0 cat 1 dog
Starting at 1 instead
Computers count from 0, but people count from 1. Give enumerate a second argument and it starts wherever you say.
Python
pets = ["cat", "dog"]
for place, pet in enumerate(pets, 1):
print(f"{place}. {pet}")
It prints
1. cat 2. dog
Try it yourself
What does this print?
Python
for i, letter in enumerate("abc"):
print(i, letter)
Why does enumerate need two names after for?
- It does not, one is enough
- It hands back two things each time round — the position and the item
- One is for the list and one is for the loop
- To make it faster
Answer them in the app
🤝 Two Lists at Once
Zipping lists together
zip walks two lists side by side, handing you one item from each — like doing up a zip.
Python
names = ["Ada", "Sam"]
scores = [10, 7]
for name, score in zip(names, scores):
print(name, score)
It prints
Ada 10 Sam 7
It stops at the shorter one
If one list runs out first, zip simply stops. No crash, no made-up values.
Python
for a, b in zip([1, 2, 3], ["x", "y"]):
print(a, b)
It prints
1 x 2 y
Building a dictionary in one line
Two matching lists plus zip plus dict gives you a dictionary straight away.
Python
names = ["Ada", "Sam"]
ages = [9, 11]
print(dict(zip(names, ages)))
It prints
{'Ada': 9, 'Sam': 11}
Try it yourself
What does this print?
Python
for a, b in zip("abc", [1, 2, 3]):
print(a, b)
Answer it in the app
🏆 Set Master
Try it yourself
What does this print?
Python
print(len(set([1, 1, 2, 2, 3])))
Answer it in the app