🗂️ Dictionaries & Sets, lesson 5 of 5
Set operations
Combine and compare sets: union, intersection and difference.
9 min
1 exercise
2 quizzes
0/3 solved
Getting Python ready… examples can run in a moment.
Sets shine when you compare groups:
a | bunion: in either seta & bintersection: in botha - bdifference: inabut notba ^ bsymmetric difference: in exactly onea <= bsubset: is everything inaalso inb?
Friends in common
Is ana - ben the same as ben - ana? Try it.
Membership table
Every name in either set gets a row. The first two columns say which set each name is in.
One row per name, ✓ = in that set. Each operator builds its column from the ana and ben columns.
Missing ingredients
Add "eggs" and "milk" to the pantry and run it again.
What does this print?
Fast membership
word in blocked stays fast for a set, even with a
million entries. With a list, Python has to check
the items one by one.
You need to look up each student's phone number by their name. Which collection fits best?
Team skills
Use set operators to compute:
shared: skills Alice and Bob both haveteam: every skill either of them hasalice_missing: skills thejobneeds that Alice doesn't havecovered:Trueif the team together has every skill the job needs