Module 8 · Python Data Structures — topic 14 of 22
Union
The union of sets contains every item that appears in at least one of them. Learn |, union() with several arguments, in-place update(), and practical uses in merging and deduplicating data.
Learning objectives
- Combine sets with | and union()
- Merge several collections at once
- Update a set in place with |= or update()
Definition
The union of sets contains every item that appears in at least one of them. Learn |, union() with several arguments, in-place update(), and practical uses in merging and deduplicating data.
Introduction
The union of two sets is a set of everything that appears in either one. Duplicates collapse, so each item appears once. {1, 2} | {2, 3} is {1, 2, 3}.
Why it matters
Merging lists of subscribers, combining tags from several articles or gathering all the skills in a team are all unions: you want everything, once.
Detailed explanation
a | b returns a new set containing every item from a and b.
a.union(b, c, d) accepts several iterables at once, including lists and tuples.
a |= b or a.update(b) adds the items of b to a in place, without creating a new set.
The size of a union is at most len(a) + len(b) and at least the larger of the two; it is smaller when they overlap.
Union is commutative: a | b equals b | a.
When order matters, for example merging lists while keeping first-seen order, a set alone isn’t enough; combine a set for “seen” with a list for order, as in the sets lesson.
Syntax
- | returns a new set.
- union() accepts many iterables.
- |= and update() change the left set.
Code examples
Union with operator and method
['Asha', 'Ben', 'Chen'] ['Asha', 'Ben', 'Dev', 'Esha']
In-place union
['git', 'python', 'sql']
More examples
Merging mailing lists
7 entries 4 unique contacts: ['a@x.com', 'b@x.com', 'c@x.com', 'd@x.com']
Step-by-step explanation
- clean() normalises each email with strip() and lower() in a set comprehension.
- Without cleaning, "B@x.com " and "b@x.com" would be counted as different.
- Chained | merges all three cleaned sets.
- Seven entries collapse to four unique contacts.
Expected output
Running “Merging mailing lists” prints exactly:
Practical applications
- Marketing tools merge contact lists from several campaigns.
- Search engines combine results from several indexes.
- Teams combine skill sets to see their total capabilities.
Best practices
- Normalise values before merging.
- Use union() to merge many iterables at once.
- Use update() to grow a set over time.
Common mistakes
Using + on sets
Avoid
Prefer
Sets don’t support +; use | or union().
Merging without normalising
Avoid
Prefer
Sets compare exact values.
Key takeaways
- Union contains items in either set, once each.
- | and union() return new sets; |= and update() change in place.
- union() accepts lists and multiple arguments.
Try it yourself
- 1Merge three lists of city names into one set and print them sorted.
- 2Keep a growing set of all words seen across several sentences using update().
Finished reading? Mark it complete to update your dashboard.
Suggested practice
13 questions cover the “Python Data Structures” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?