Set method union combines two or more sets into a new set containing all unique elements from the inputs — the mathematical union, with duplicates automatically removed (since sets have no duplicates). The union() method (or the | operator) is fast, readable, and ideal for merging collections, deduplicating data, aggregating unique IDs, or building lookup tables. In 2026, set union remains a go-to operation — used constantly in data processing, validation, feature engineering, and production code where you need fast membership checks and set algebra without loops or temporary lists.
Here’s a complete, practical guide to set union: basic union() and |, multiple sets, real-world patterns, performance advantages, and modern best practices with type hints and safety.
The core method set1.union(set2) (or set1 | set2) returns a new set with all unique elements from both — order doesn’t matter, and originals are unchanged.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # {1, 2, 3, 4, 5}
# Equivalent operator syntax (often more readable)
union_set2 = set1 | set2
print(union_set2) # {1, 2, 3, 4, 5}
Multiple sets are supported — union() combines all inputs; the | operator chains left-to-right.
set3 = {5, 6, 7}
union_multi = set1.union(set2, set3)
print(union_multi) # {1, 2, 3, 4, 5, 6, 7}
# Chain with operator
union_multi2 = set1 | set2 | set3
print(union_multi2) # same result
Real-world pattern: merging unique items from multiple sources — perfect for combining user IDs, keywords, categories, or deduplicating logs/API results.
active_users = {"alice", "bob", "charlie"}
premium_users = {"bob", "david", "eve"}
all_users = active_users | premium_users
print("All users:", all_users) # {'alice', 'bob', 'charlie', 'david', 'eve'}
# From lists (convert to set first)
raw_ids1 = [1, 2, 3, 2, 4]
raw_ids2 = [3, 5, 6]
unique_ids = set(raw_ids1) | set(raw_ids2)
print("Unique IDs:", unique_ids) # {1, 2, 3, 4, 5, 6}
Best practices make set union fast, safe, and readable. Prefer | operator for two sets — set1 | set2 is concise and readable; use union() for multiple sets or chaining. Convert lists to sets only when needed — set(lst1) | set(lst2) removes duplicates automatically. Add type hints for clarity — set[int] or set[str] — improves readability and mypy checks. Modern tip: use Polars for large tabular data — pl.concat([df1, df2]).unique() or df1.join(df2, how="outer") often replaces manual set union efficiently. In production, wrap union over external data (files, APIs) in try/except — handle invalid items gracefully. Use update() for in-place union when you don’t need the original set — set1.update(set2) modifies set1 directly. Combine with Counter — Counter(a) | Counter(b) for multiset union (max counts).
Set union with union() or | is Python’s clean, fast way to merge unique elements — memory-efficient, O(1) lookups, and perfect for aggregation and deduplication. In 2026, use operator syntax for two sets, chain for multiple, type hints for safety, and Polars for big data. Master set union, and you’ll combine, merge, and aggregate collections with speed and clarity.
Next time you need all unique elements from multiple sources — reach for set union. It’s Python’s cleanest way to say: “Give me everything, no duplicates.”