S1Q3 · Merge and Remove Duplicates¶
⚡ Quick Reference
Function: merge_and_remove_duplicates(list1: list, list2: list) -> set
Core idea: merge both lists, convert to a set - sets automatically remove duplicates.
Key rules:
- list1 + list2 concatenates the two lists
- set(...) removes all duplicates automatically
- Order is not preserved in a set - that's expected
Problem Statement¶
Problem
Write a function merge_and_remove_duplicates(list1: list, list2: list) -> set that merges both lists and returns a set containing all unique elements from both.
Examples:
list1 = [1, 2, 2]
list2 = [2, 1, 1]
{1, 2}
list1 = ["ant", "bat"]
list2 = ["bat", "cat"]
{'ant', 'bat', 'cat'}
Note on the first example
The problem statement shows {1, 2, 1} as output, but a set cannot contain duplicates - {1, 2, 1} is not a valid set. The correct output is {1, 2}. This appears to be a typo in the question.
Understanding the problem¶
A set in Python is an unordered collection of unique elements. Converting any iterable to a set automatically drops all duplicates - which is exactly what this problem asks for.
| Step | Operation | Result |
|---|---|---|
| Concatenate | [1, 2, 2] + [2, 1, 1] |
[1, 2, 2, 2, 1, 1] |
| Remove duplicates | set([1, 2, 2, 2, 1, 1]) |
{1, 2} |
Key insight
Sets are defined by uniqueness. The moment you call set() on anything, every duplicate vanishes. You don't need to write any loop or comparison logic - the data structure does the work.
Tracing the examples¶
Example 1: list1 = [1, 2, 2], list2 = [2, 1, 1]
Example 2: list1 = ["ant", "bat"], list2 = ["bat", "cat"]
"bat" appeared twice - once from each list - but the set keeps only one copy.
Solution approaches¶
Concatenate with +, wrap in set(). Clean and direct.
Convert each list to a set first, then use the | (union) operator to combine them. The union of two sets contains all elements from both, with no duplicates. Slightly more explicit about the intent.
Same as union but using the method form. set.union() accepts any iterable as argument - no need to convert list2 to a set first.
def merge_and_remove_duplicates(list1: list, list2: list) -> set:
result = set()
for item in list1 + list2:
result.add(item)
return result
Manually add each element to a set. The set's uniqueness constraint handles duplicates silently - add() on an existing element is simply a no-op. More verbose but makes the mechanics visible.
Set operations - quick reference¶
| Operation | Symbol | Method | Meaning |
|---|---|---|---|
| Union | A \| B |
A.union(B) |
All elements from both |
| Intersection | A & B |
A.intersection(B) |
Only elements in both |
| Difference | A - B |
A.difference(B) |
In A but not in B |
| Symmetric diff | A ^ B |
A.symmetric_difference(B) |
In one but not both |
For this problem, union is the right operation.
Key takeaways¶
set() removes duplicates instantly
Any time a problem says "unique elements" or "remove duplicates", set() is almost always the answer. No loops needed.
Three ways to merge into a set
set(l1 + l2), set(l1) | set(l2), and set(l1).union(l2) are all equivalent. Know all three.
Sets are unordered
A set has no guaranteed order. {1, 2} and {2, 1} are the same set. Don't rely on insertion order when working with sets.