The union method
The union
method returns a set obtained by combining the elements of the sets specified in the parameter.
Syntax
set.union(sets we want to unite)
Example
Let's apply the union
method to get the common elements of two sets:
st1 = {'a', 'b', 'c'}
st2 = {'1', '2', '3'}
res = st1.union(st2)
print(res)
Result of code execution:
{'c', '2', 'b', '1', 'a', '3'}
Example
The union
method also has a short form:
st1 = {'a', 'b', 'c'}
st2 = {'1', '2', '3'}
res = st1 | st2
print(res)
Result of code execution:
{'a', '1', 'b', '3', '2', 'c'}