You have two lists of tag strings — ["python", "web", "data"] and ["data", "ai", "python"] — and you want only the tags that appear in both: ["data", "python"]. Sorted, deduplicated. What's the cleanest operation?
A nested loop — for each tag in list A, check if it's in list B? That feels slow.
It works but it's O(n*m). Sets turn it into O(n+m) and the code becomes a single expression. Python's & operator on two sets returns their intersection — the elements present in both:
set(["python", "web", "data"]) & set(["data", "ai", "python"])
# {"data", "python"}The math is built into the language. No loop, no if in checks.
And sorting the result gives a deterministic list for tests and display?
Right — sets have no order, so wrap with sorted() for a predictable list:
def find_common_tags(tags_a: list, tags_b: list) -> list:
return sorted(set(tags_a) & set(tags_b))Convert to sets, intersect, sort. Three operations, one expression.
What if both lists have duplicates — does the result dedupe automatically?
Yes. set(["a", "a", "b"]) is {"a", "b"} — duplicates collapse on conversion. So the intersection is always deduplicated, regardless of duplicate tags in the inputs. And if there are no common elements, & returns an empty set, which sorted() turns into [].
So this same pattern works for any "what's shared" question — common users across two systems, overlapping skills across two job posts, shared ingredients across recipes.
That's the pattern. Whenever you see "items in both" or "common to" or "intersection of" — reach for set(a) & set(b). The operator names the intent directly.
TL;DR: set(a) & set(b) returns elements present in both — O(n+m) time, auto-deduplicated.
& operator — intersection; returns a setsorted(result) — deterministic list for display/testing| Operator | Meaning |
|---|---|
a & b | intersection (in both) |
a | b | union (in either) |
a - b | difference (in a, not b) |
a ^ b | symmetric difference (in one, not both) |
Same concepts you'd find in a math textbook — Python just gives them operators.
You have two lists of tag strings — ["python", "web", "data"] and ["data", "ai", "python"] — and you want only the tags that appear in both: ["data", "python"]. Sorted, deduplicated. What's the cleanest operation?
A nested loop — for each tag in list A, check if it's in list B? That feels slow.
It works but it's O(n*m). Sets turn it into O(n+m) and the code becomes a single expression. Python's & operator on two sets returns their intersection — the elements present in both:
set(["python", "web", "data"]) & set(["data", "ai", "python"])
# {"data", "python"}The math is built into the language. No loop, no if in checks.
And sorting the result gives a deterministic list for tests and display?
Right — sets have no order, so wrap with sorted() for a predictable list:
def find_common_tags(tags_a: list, tags_b: list) -> list:
return sorted(set(tags_a) & set(tags_b))Convert to sets, intersect, sort. Three operations, one expression.
What if both lists have duplicates — does the result dedupe automatically?
Yes. set(["a", "a", "b"]) is {"a", "b"} — duplicates collapse on conversion. So the intersection is always deduplicated, regardless of duplicate tags in the inputs. And if there are no common elements, & returns an empty set, which sorted() turns into [].
So this same pattern works for any "what's shared" question — common users across two systems, overlapping skills across two job posts, shared ingredients across recipes.
That's the pattern. Whenever you see "items in both" or "common to" or "intersection of" — reach for set(a) & set(b). The operator names the intent directly.
TL;DR: set(a) & set(b) returns elements present in both — O(n+m) time, auto-deduplicated.
& operator — intersection; returns a setsorted(result) — deterministic list for display/testing| Operator | Meaning |
|---|---|
a & b | intersection (in both) |
a | b | union (in either) |
a - b | difference (in a, not b) |
a ^ b | symmetric difference (in one, not both) |
Same concepts you'd find in a math textbook — Python just gives them operators.
Write `find_common_tags(tags_a, tags_b)` that returns a sorted list of tag strings present in both input lists. Use set intersection.
Tap each step for scaffolded hints.
No blank-editor panic.