Open In App

Python - Union of Two or More Lists

Last Updated : 26 Nov, 2025
Comments
Improve
Suggest changes
6 Likes
Like
Report

Given two or more lists, the task is to combine all elements into a single list. If unique elements are required, duplicates must be removed while forming the union. For Example:

Input: a = [1, 2, 3], b = [3, 4, 5]
Output: [1, 2, 3, 4, 5]

Let's explore different methods to find union of two list in Python.

Using set.union()

set.union() merges multiple sets and returns all unique values. By converting lists into sets and applying union(), duplicates are removed automatically.

Python
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
u = list(set(a).union(b))
print(u)

Output
[1, 2, 3, 4, 5, 6]

Explanation:

  • set(a) creates a set from list a.
  • .union(b) adds all elements of b while discarding duplicates.
  • list(...) converts the result back to a list.

Using | Operator

The | operator performs the same operation as set.union() but in a shorter form. Both sets are merged, and duplicate values are removed automatically.

Python
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
u = list(set(a) | set(b))
print(u)

Output
[1, 2, 3, 4, 5, 6]

Explanation:

  • set(a) and set(b) convert both lists to sets.
  • | merges them while keeping only unique elements.
  • Final list is formed using list(...).

Using NumPy union1d()

For numerical lists, NumPy provides a highly optimized union method union1d(). It returns sorted unique elements from both lists.

Python
import numpy as np

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
u = np.union1d(a, b)
print(u)

Output
[1 2 3 4 5 6]

Explanation: np.union1d(a, b) merges both inputs.

Using List Concatenation

This method simply combines both lists without removing duplicates.

Python
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
u = a + b
print(u)

Output
[1, 2, 3, 4, 3, 4, 5, 6]

Explore