Set Union
tags: #python/documentation/sets
The union of two or more sets is the set containing all the elements of the given sets. We can do this by using the union() method.
set1 = set(iteratble)
set2 = set(iterable)
setAandB = set1.union(set2, ...)
This returns a set that contains all items from the original set, and all items from the specified set(s).
How many sets can we union?
You can specify as many sets you want, separated by commas. Can also join with any iterable object, does not have to be a set.
What if an item is present in more than 1 set?
The element will only appear once in the union set.
Mathematical Representation
The union of set A and set B is equal to the set containing all the elements in A and B, less the elements that are common to both A and B (cardinality of set A ∩ B, i.e. A intersection B).
Example:
x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z)
print(result)
Output:
{'e', 'c', 'b', 'd', 'f', 'a'}