Combining Python Sets: Operators and Methods
Using the | Operator
The pipe (|) operator provides a concise syntax for calculating the union of two or more sets. It returns a completely new set containing all distinct elements from the operands.
fruits_a = {"apple", "banana", "cherry"}
fruits_b = {"cherry", "dates", "elderberry"}
all_fruits = fruits_a | fruits_b
print(all_fruits) # Output: {'apple', 'banana', 'cherry', 'dates', 'elderberry'}
Using the union() Method
Similar to the pipe operator, the union() method produces a new set containing the combined unique elements. It accepts one or more iterable arguments, offering flexibility when merging multiple collections.
primary_colors = {"red", "blue", "yellow"}
secondary_colors = {"blue", "green", "orange"}
palette = primary_colors.union(secondary_colors)
print(palette) # Output: {'red', 'blue', 'yellow', 'green', 'orange'}
Since sets eliminate duplicates automatically, overlapping elements appear only once. The original collections remain unmodified unless the result is reassigned:
primary_colors = primary_colors.union(secondary_colors)
print(primary_colors) # Output: {'red', 'blue', 'yellow', 'green', 'orange'}
In-Place Merging with update()
When memory efficiency is a concern and mutating the original set is acceptable, update() adds elements from an iterable directly into the existing set. Unlike union(), it alters the caller in place.
european_cities = {"Paris", "Berlin"}
asian_cities = {"Tokyo", "Paris", "Seoul"}
european_cities.update(asian_cities)
print(european_cities) # Output: {'Paris', 'Berlin', 'Tokyo', 'Seoul'}
Combining Sets via Comprehensions
Set comprehensions allow for the dynamic generation of sets based on conditions. These derived sets can then be combined using standard union operations.
raw_scores = [12, 15, 22, 33, 40, 51]
passing = {s for s in raw_scores if s >= 20}
failing = {s for s in raw_scores if s < 20}
all_scores = passing | failing
print(all_scores) # Output: {33, 40, 12, 15, 51, 22}
Chaining Multiple Sets with itertools
For scenarios involving numerous sets, itertools.chain() creates an iterator that yields elements from the provided iterables sequentially. Converting this iterator to a set yields the final unified collection.
import itertools
group_alpha = {"A1", "A2"}
group_beta = {"B1", "B2"}
group_gamma = {"C1", "C2"}
merged_iterator = itertools.chain(group_alpha, group_beta, group_gamma)
final_set = set(merged_iterator)
print(final_set) # Output: {'A1', 'A2', 'B1', 'B2', 'C1', 'C2'}