🗂️ 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?
Example
Friends in common
Is ana - ben the same as ben - ana? Try it.
Example
Missing ingredients
Add "eggs" and "milk" to the pantry and run it again.
Quiz
What does this print?
5 2 2
7 2 2
5 2 1
4 2 2
Example
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.
Quiz
You need to look up each student's phone number by their name. Which collection fits best?
A list
A set
A dict
A tuple
Exercise
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