HOWTO · Python

How to Get Union of Lists in Python

Union of Lists reflects that all the elements from the different lists will be put up inside a single list.

On this page

In Python, + concatenates lists: it places the second list’s items after the first list’s items and keeps repeated values. This differs from mathematical set union, which keeps each distinct value once and does not guarantee an order. Choose concatenation when order and repetitions matter, add sorted() when you need ordered values, or use set() when you need distinct hashable values. The examples below are self-contained and show the resulting values.

Combine Two Lists with + and Keep Duplicates

Use + to combine two lists while preserving their order and every occurrence. The result is a new list, so both input lists remain available for later use.

first = ["red", "blue"]
second = ["blue", "green"]

combined = first + second
print(combined)
['red', 'blue', 'blue', 'green']

The resulting list contains the items from first followed by those from second; "blue" appears twice because it occurs in both inputs. This is list concatenation, rather than a unique-value union.

Sort the Combined Lists

Concatenate the lists first, then pass the result to Python’s built-in sorted() function. It returns a new list arranged in ascending order and leaves the inputs unchanged. Sorting does not remove duplicates, so each occurrence remains in the result.

first = [11, 20, 1, 2, 3, 4, 5]
second = [2, 4, 6, 8, 10]

combined = first + second
sorted_combined = sorted(combined)
print(sorted_combined)
[1, 2, 2, 3, 4, 4, 5, 6, 8, 10, 11, 20]

For numbers, the values are ordered numerically. The values being sorted must be comparable with one another; for example, Python does not define a general ascending order between integers and strings.

Combine Two Lists and Remove Duplicates

Convert the concatenated list to a set with set(), then convert that set back to a list if the rest of your code needs a list. A set retains one copy of each distinct value, but its iteration order is not guaranteed. Its elements must be hashable, so this approach works for values such as numbers and strings, but not nested lists.

first = [1, 2, 3, 4, 5]
second = [2, 4, 6, 8, 10]

unique_values = list(set(first + second))
print(unique_values)
[1, 2, 3, 4, 5, 6, 8, 10]

Use this approach when unique membership matters more than position. If the combined values must also be sorted, sort the resulting list explicitly with sorted().

Combine More Than Two Lists and Remove Duplicates

For several inputs, call union() on a set and pass each list as an argument. union() is a set method; it accepts multiple iterables and returns a set containing each distinct hashable value. Convert the result to a list when a list is the required return type. As with the two-list example, the output order is not guaranteed.

first = [1, 2, 3, 4, 5]
second = [2, 4, 6, 8, 10]
third = [5, 6, 7, 8, 11, 15, 18]

unique_values = list(set().union(first, second, third))
print(unique_values)
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 15, 18]

Add another list by passing it as another argument to union(). If the values are unhashable, convert them to a suitable hashable representation or use a different deduplication strategy; sets cannot directly contain mutable lists or dictionaries.