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).

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).

(AB)=A+B(AB)

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'}
Powered by Forestry.md