Sets for membership and overlap
A set stores unique values and answers "is x in here?" instantly. Asking the same question of a list scans it from the start, so doing it thousands of times gets slow.
Sets also have operators for comparing groups:
a & bvalues in both (intersection)a | bvalues in either (union)a - bvalues inabut not inb(difference)
mine = {"fox", "owl"}
yours = {"owl", "bee"}
mine & yours # {"owl"}Sets have no order and no duplicates. When the answer must be a list, wrap it in sorted(...) so it is always in the same order. To intersect many sets, start with one and keep applying &=.
In the visual, predict what both holds before the line runs.
See it run
mine = {"fox", "owl", "elk"}
yours = {"owl", "bee", "elk"}
both = mine & yours
only_mine = mine - yoursTwo sets of animal names. Operators compare them.
Practice sets for membership and overlap
Exercise 1
Two party guest lists, first and second, hold names (strings). Some people appear on both, and a name may be written more than once on the same list.
Return how many different names appear on both lists, as an integer.
Rules:
- Names are case-sensitive.
- If either list is empty, return
0.
Constraints: 0 ≤ len(first), len(second) ≤ 10^5.
Example: solution(["x", "x"], ["x", "y"]) returns 1 because only "x" is on both lists, even though it is listed twice.
Exercise 2
A to-do app has the ids of tasks still planned in planned and the ids already finished in finished. Both are lists of integers and may contain repeats.
Return a list of the task ids that are in planned but not in finished, with each id once, sorted from smallest to largest.
Rules:
- If every planned task is finished, return
[]. - Ids can be zero or negative.
Constraints: 0 ≤ len(planned), len(finished) ≤ 10^5.
Example: solution([5, 3, 5, 9], [3]) returns [5, 9].
Exercise 3
A class keeps attendance. days is a list with one list of names for each school day; a name can be written more than once on the same day.
Return a list of the names that were present on every day, sorted alphabetically, each name once.
Rules:
- A day with an empty list means nobody was present that day.
- If
daysitself is empty, return[].
Constraints: 0 ≤ len(days) ≤ 1000; each day has at most 1000 names.
Example: solution([["ann","bo"], ["bo","cy","ann"], ["ann","bo","bo"]]) returns ["ann", "bo"].
Exercise 4
A door reader logs every badge id that is swiped in attempts. blocked lists the ids that are not allowed in. Both are lists of integers.
Return how many entries of attempts use a blocked id. Repeated attempts each count.
Rules:
blockedmay be empty, and may contain repeated ids.- Empty
attemptsgives0.
Constraints: 0 ≤ len(blocked) ≤ 30000; 0 ≤ len(attempts) ≤ 9000.
Example: solution([4, 8, 15], [8, 16, 8, 4, 23]) returns 3: the two 8s and the 4.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.