Python programmers have several methods for combining two sets into one. In this blog post, we'll explore the set `union()`

method, and we'll look at some examples of how to use it. We'll also discuss some of the benefits of using set `union()`

, and we'll see why it's a popular tool for Python developers.

Finally, we'll go over some advanced use cases and examine the efficiency of set `union()`

. So let's get started!

Did you know Python now supports switch statements? Here's a complete step-by-step guide to get you started.

Don't feel like reading? Watch my video instead:

## Python Set Union - The Basics

In this section, we'll go over the basic definition and usage of the set `union()`

function in Python, explore its syntax and return value.

### Definition and Usage of Set Union in Python

Before we get into some examples, let's start with the basics. What is a set `union()`

? As you might expect, it's a function that combines two sets into one. The function can combine one set with multiple other sets or Python iterable objects.

For example, take a look at the following two sets - **A** and **B**:

Calculating a union between these sets means we want to create a new set that has all distinct elements from both:

Python set `union()`

is oftentimes represented with a Venn diagram. Here's what it would look like:

The set on the left has elements *R* and *Julia* that aren't present in the set on the right. Likewise, the set on the right has *JavaScript* and *Go* as distinct elements. There's one element common to both sets - *Python*.

**Keep in mind**: If an item is present in more than one set, the resulting set will list the item only once.

### Syntax of Set Union in Python

```
# Combine two sets
set1.union(set2)
# Combine multiple sets
set1.union(set2, set3, ...)
```

Where:

`set1`

- The iterable to unify with.`set2`

,`set3`

- Optional set(s), other iterables to unify with.

### Return value of Set Union in Python

The set `union()`

function in Python returns a new set which is the union of all sets with the first one - but only if set(s) or iterable object(s) were passed to the `union()`

function.

If no arguments were passed into the `union()`

function, a copy of the set is returned.

## Python Set Union Function Example

We'll declare two sets, just as on the above images:

`A`

- Contains programming languages used in data science`B`

- Contains programming languages used in web development

Some programming languages are interchangeable, like Python, so it's present in both sets. It should only get printed once, as we've seen before:

```
A = {'Python', 'R', 'Julia'}
B = {'Python', 'JavaScript', 'Go'}
print(f"A U B = {A.union(B)}")
```

Output:

`A U B = {'Go', 'Python', 'JavaScript', 'R', 'Julia'}`

If you don't specify any parameters to the Python set `union()`

function, the set gets copied:

`print(f"A.union() = {A.union()}")`

Output:

`A.union() = {'R', 'Julia', 'Python'}`

You can verify it was copied by printing its memory address:

```
A = {'Python', 'R', 'Julia'}
A_copy = A.union()
print(hex(id(A)))
print(hex(id(A_copy)))
```

Output:

```
0x105a03e40
0x105a039e0
```

You won't see the identical values, and that's not the point. The important thing is that they're different, indicating the set was copied to a different memory address.

Let's now explore a shorter way to get the union of multiple sets.

## Python Set Union Using the | Operator

There's no need to call the Python set `union()`

function every time. You can use the pipe (`|`

) operator instead:

```
A = {'Python', 'R', 'Julia'}
B = {'Python', 'JavaScript', 'Go'}
print(f"A U B = {A | B}")
```

Output:

`A U B = {'Go', 'Python', 'JavaScript', 'R', 'Julia'}`

Everything else remains the same. This approach is more compact and readable than the first one, at least if you're combining two sets.

## Python Set Union Advanced Examples

We'll now go over a couple of "advanced" union examples and use cases:

- Multiple set arguments
- Set update vs. set union
- Python union on iterable objects

### Multiple Set Arguments

You can get the union of one set with multiple sets. We'll declare yet another set containing programming languages used in scientific computing and calculate the union of all three.

The calculation works both by using the regular syntax and the shorthand pipe syntax:

```
A = {'Python', 'R', 'Julia'}
B = {'Python', 'JavaScript', 'Go'}
C = {'R', 'Matlab', 'Octave'}
print(f"A U B U C = {A.union(B, C)}")
print(f"A U B U C = {A | B | C}")
```

Output:

```
A U B U C = {'Octave', 'Go', 'Python', 'JavaScript', 'Matlab', 'R', 'Julia'}
A U B U C = {'Octave', 'Go', 'Python', 'JavaScript', 'Matlab', 'R', 'Julia'}
```

### Set Update vs. Union

You can perform the union operation in Python both with `set.update()`

and `set.union()`

. The first one adds all missing elements to the set on which it is called and returns `None`

, while the latter creates and returns a new set.

Here's how `set.update()`

works:

```
A = {'Python', 'R', 'Julia'}
B = {'Python', 'JavaScript', 'Go'}
A.update(B)
print(A)
```

Output:

`{'Go', 'Python', 'JavaScript', 'R', 'Julia'}`

As you can see, the update happens in place. You **can't** save the results of the update operation to a new set, so keep that in mind:

```
A = {'Python', 'R', 'Julia'}
B = {'Python', 'JavaScript', 'Go'}
C = A.update(B)
print(C)
```

Output:

`None`

### Python Union on Iterable Objects

You can call the `union()`

function on a Python set and provide any iterable object as an argument - here's an example for a Python list:

```
l1 = {1, 2, 3}
l2 = [2, 3, 4]
print(l1.union(l2))
```

Output:

`{1, 2, 3, 4}`

**Keep in mind**: You can't use the shorthand pipe operator:

```
l1 = {1, 2, 3}
l2 = [2, 3, 4]
print(l1 | l2)
```

Output:

You also can't use anything but a Python set first - as no other data type has the `union()`

function:

```
l1 = [1, 2, 3]
l2 = [2, 3, 4]
print(l1.union(l2))
```

Output:

**Long story short** - You always have to use the `union()`

function instead of the pipe operator and you must call the function on a set.

## Python Set Union Performance (Time Complexity)

We'll now analyze the time complexity of the set union operation in Python. I've found the source code over at Finxter blog and slightly modified it. To summarize:

- Time complexity on a set with
**n**elements and set arguments with**m**elements is*O(n + m)*. - Inserting an element into a set has a complexity of
*O(1)*.

Here's the Python code that calculates and displays a figure of set size on the X-axis and runtime in seconds on the Y-axis. I've run the code on M1 Pro MacBook Pro 16":

```
import time
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (12, 5)
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.right'] = False
# Calculations
sizes = [i * 10**5 for i in range(100)]
runtimes = []
for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))
t1 = time.time()
s.union(t)
t2 = time.time()
runtimes.append(t2 - t1)
# Plot
plt.figure()
plt.plot(sizes, runtimes, lw=3, color='#101010')
plt.title('Python set union() runtime vs. set size', size=20)
plt.xlabel('Set size', size=14)
plt.ylabel('Runtime (s)', size=14);
```

Output:

## Conclusion

Python set union is simple to understand. We went through the definition and intuition, and slowly built our way towards understanding more complicated use cases. You have to admit - even the *advanced* section was easy to digest.

I hope that this article has helped you develop a better understanding of the Python set union function. As always, if you have any questions or comments, please feel free to ask in the comment section below. Happy coding!

### Learn More

- Python If-Else Statement in One Line - Ternary Operator Explained
- Python Single vs. Double Quotes - Which Should You Use and Why?
- Dask Delayed - How to Parallelize Your Python Code With Ease

### Stay connected

- Sign up for my newsletter
- Subscribe on YouTube
- Connect on LinkedIn