We use union() method in Python to find the union of two or more sets (or any iterable).
The union() method returns a set that contains all items from the original set, and all items from the specified set (s) excluding the duplicates.
Syntax
If there are two sets, set1 and set2, then, union of set1 U set2 is given as:
set1.union(set2)
We can also specify two or more sets,
set1.union(set2, set3, set4, ...)
Let’s see one example for more clear understanding,
s1 = {1, 2, 3, 4, 5}
s2 = {'one', 'two', 'three'}
print(s1.union(s2))
O/P
{1, 2, 3, 4, 5, ‘two’, ‘one’, ‘three’}
Union of multiple Sets
s1 = {1, 2, 3, 4, 5}
s2 = {'one', 'two', 'three'}
s3 = {1, 'one', 'TWO', 6}
print(s1.union(s2, s3))
O/P
{1, 2, 3, 4, 5, 6, ‘three’, ‘TWO’, ‘two’, ‘one’}
That’s all here.
