# Python set union

The Python set union function combines all the items in the original set and the specified set. The syntax of this set union method is

`set.union(set1, set2,…,setN)`

## Python set union example

In this example, we declared two sets. Next, we are combining those two sets using the Python set union method.

```# Python set union
x = {'a', 'b', 'c', 'd'}
y = {'e', 'f', 'g', 'h'}

print('Set Items in x = ', x)
print('Set Items in y = ', y)

z = x.union(y)

print('Set Items in z = ', z)```

set union function output

``````Set Items in x =  {'c', 'd', 'b', 'a'}
Set Items in y =  {'f', 'h', 'e', 'g'}
Set Items in z =  {'f', 'h', 'd', 'g', 'c', 'b', 'a', 'e'}``````

The Python sets don’t allow duplicate values inside a set. So, while using the union method to combine the two sets, it removes those duplicate values. If you observe the output, a and c was returned only once (though, they are available in both x and y sets).

TIP: Please refer to the Python set in Python.

```x = {'a', 'b', 'c', 'd'}
y = {'a', 'f', 'g', 'c', 'h', 'j', 'k'}

print('Set Items in x = ', x)
print('Set Items in y = ', y)

z = x.union(y)

print('Set Items in z = ', z)```

set union output

``````Set Items in x =  {'b', 'a', 'c', 'd'}
Set Items in y =  {'k', 'j', 'g', 'a', 'c', 'h', 'f'}
Set Items in z =  {'b', 'k', 'a', 'd', 'j', 'g', 'c', 'h', 'f'}``````

### Python union example 2

Let me use three sets with union function. This Python set union example, combines the set time from all the three sets. It first combines items in x and then y and z.

```x = {'a', 'b' }
y = {'e', 'f', 'g', 'h'}
z = {'apple', 'cherry', 'kiwi'}

print('Set Items in x = ', x)
print('Set Items in y = ', y)
print('Set Items in z = ', z)

new_set = x.union(y, z)

print('Set Items in new = ', new_set)```

set union output

``````Set Items in x =  {'a', 'b'}
Set Items in y =  {'h', 'g', 'e', 'f'}
Set Items in z =  {'kiwi', 'apple', 'cherry'}
Set Items in new =  {'kiwi', 'g', 'h', 'apple', 'e', 'a', 'f', 'b', 'cherry'}
``````

This set union example shows how to combine numeric and string sets.

```x = {'a', 'b' }
y = {10, 20, 30, 40}

print('Set Items in x = ', x)
print('Set Items in y = ', y)

z = x.union(y)

print('Set Items in z = ', z)```

union function to combine numeric and string sets output

``````Set Items in x =  {'b', 'a'}
Set Items in y =  {40, 10, 20, 30}
Set Items in z =  {20, 'b', 40, 10, 30, 'a'}``````

Here, we declare a numeric set, string set, and a mixed set. Next, we used the set union method to combine them all and returning a new set.

```x = {'a', 'b' }
y = {10, 20, 30, 40}
z = {120, 'cherry', 90, 'kiwi'}

print('Set Items in x = ', x)
print('Set Items in y = ', y)
print('Set Items in z = ', z)

new_set = x.union(y, z)

print('Set Items in new = ', new_set)```