Dark Mode On/Off

# Join two Sets in Python

In this article, we will learn to join two or more sets in Python. We will use some built-in functions, some simple approaches, and some custom codes as well to better understand the topic. Let's first have a quick look over what is a set and how it is different from a list in Python.

## Python Set

Python has a built-in data type called set. It is a collection of unordered data values. Due to the unordered dataset, the user is not sure of the order in which data values appear. An unordered dataset leads to unindexed values. Set values cannot be accessed using index number as we did in the list. Set values are immutable which means we cannot alter the values after their creation. Data inside the set can be of any type say, integer, string, or float value. The set uses comma-separated values within curly brackets `{} `to store data. Sets can be defined using any variable name and then assigning different values to the set in the curly bracket. The set is unordered, unchangeable, and does not allows duplicate values.

``````set1 = {"Ram","Arun","Kiran"}
set2 = {16,78,32,67}
set3 = {"apple","mango",16,"cherry",3}``````

## Join two Sets in Python

Joining two or more sets in Python basically means the union of all the sets. As we learned in mathematics that union combines all the elements from different sets into one set. We know that set does not allow duplicate members, so after the union of sets, only unique elements will be printed. This is a class of operation called a "set operation", which Python sets provide convenient tools for.

Let us look at the below examples and learn what are the different ways to join or combine two or multiple sets into one set.

1. Using union() function
2. Using update() function
3. Using " | " operator
4. Using reduce() function
5. Using itertools.chain() function
6. Using " * " operator
7. By converting sets to lists

## Example: Join sets using union() function

This method uses `union()` to join two or more sets. It returns the union of sets A and B. It does not modify set A in place but returns a new resultant set. The union is the smallest set of all the input sets taken and it does not allow any duplicate elements too.

``````set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

#new set
set3 = set1.union(set2)
print(set3)``````

{"a","b","c",1,2,3}

union() function can also be used in a different way. Look at the below example-

### Example 2

``````set1 = {1, 3}
set2 = {5}
set3 = {7, 9}

new_set = set().union(set1).union(set2).union(set3)
print(new_set)``````

{1,3,5,7,9}

## Example: Join sets using the" | " Operator

This operator is called the union operator and works similarly to the `union()` function. It joins two or more sets and returns a new set of unique elements.

``````set1 = {'a', 'c', 'd'}
set2 = {'c', 'd', 2 }
set3 = {1, 2, 3}

print('Join set1 and set2- ', set1 | set2)
print('Join set2 and set3 =', set2 | set3)
print('Join set1 ,set2, and set3- ', set1 | set2 | set3)``````

Join set1 and set2- {"a", "c", "d", 2}
Join set2 and set3- {"c", "d", 2 , 3}
Join set1, set2 and set3- {"a", "c", "d", 2, 1, 3}

## Example: Join sets using update() Function

The `update()` method inserts all the items from one set into another and automatically does not allow duplicate elements to enter. This method joins two sets at a time.

``````set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)``````

{1, 2, 3, 'c', 'a', 'b'}

In this case, if you want to create a new set, you first need to use the copy() function on the first set and then store the result in a new set. Apply update function on this new set and a second input set was taken.

``````set1 = {1, 4, 2, 5}
set2 = {8, 2, 1, 6}

set3 = set1.copy()
set3.update(set2)

print(set3)``````

{1,4,2,5,8,6}

## Example: Join sets using reduce() Function

This reduce function imports `operator `and we need to use `functools.reduce` module to join two sets in Python. It returns the bitwise `or` of set1 and set2 which is similar to the union function in mathematical language.

``````import operator
from functools import reduce

set1 = {4, 5, 6, 7}
set2 = {6, 7, 8, 9}

print(reduce(operator.or_, [set1, set2]))``````

{4, 5, 6, 7, 8, 9}

## Example: Join sets using itertools.chain() Function

The `itertools` module provides several useful functions to create iterators for efficient looping. In this case, we use its` chain() `function to join two or more sets together as shown below in the example.

``````import itertools

set1 = {1, 3}
set2 = {5}
set3 = {7, 9}
new_set = set(itertools.chain(set1, set2, set3))
print(new_set)``````

{1, 3, 5, 7, 9}

## Example: Join sets using " * " Operator

With Python 3.5, you can use the` * `iterable unpacking operator for joining multiple sets. It is called the unpacking operator because * unpack the set. `Unpacking` is where an iterable (e.g. a set or list) is represented as every item it yields because the set can only contain unique items.

``````import itertools

set1 = {1, 3}
set2 = {5}
set3 = {7, 9}

new_set = (*set1, *set2, *set3)

print(new_set)``````

{1, 3, 5, 7, 9}

## Conclusion

In this article, we learned to join two or more sets by using several built-in functions such as `update()`, `union()`, `reduce()` and operators like` |`, `*` etc and we used some custom codes as well. We learned about the difference between lists and sets and how we can convert a set to a list. We saw several examples of the sets showing that it is an unordered data type.