Home >>Python Set Methods >Python Set union() Method

Python Set union() Method

Python Set union() Method

Python set union() method is used to return a set that contains all items from the original given set and all items from the specified sets. You can specify as many sets as you want separated by commas(,).

Syntax:
set.union(set1, set2...)

Parameter Values

Parameter Description
set1 This is a required parameter. It defines the set to unify with.
set2 This is an optional parameter. It defines the other set to unify with.
Here is an example of Python union() method:

x = {"Abhi", "Jerry", "Mickey"}
y = {"Wings", "Alpha", "Jerry"}
z = x.union(y) 
print(z)

Output:
{'Mickey', 'Alpha', 'Jerry', 'Abhi', 'Wings'}
Example 2:

x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z) 
print(result)

Output:
{'f', 'e', 'd', 'c', 'a', 'b'}

No Sidebar ads