The Python set union method is a **set method**, which is used to combine all the items in the original set and the specified set. The syntax behind this **Python** set union method is

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

In this article, we will show you, how to use this python set union method with multiple examples.

**TIP: **Please refer **Python set** article to understand everything about Sets.

## Python set union example

In this example, we declared two sets. Next, we are combining those two sets using the 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)

**OUTPUT**

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

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)

**OUTPUT**

### Python union example 2

Until now, we are working with two sets. Let me use three sets with union function. This 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)

**OUTPUT**

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

**OUTPUT**

Here, we declares a numeric set, string set and a mixed set. Next, we used the set union method to combine them all and retuning 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)

**OUTPUT**

Thank You for Visiting Our Blog