Python Program to Merge Two Lists

In this article, we will show how to write a Python program to merge elements in two lists with examples. There are multiple options to merge two lists, it includes the + operator (concatenation),  extend function, * operator, for loop, while loop, list comprehension, and chain function.

Python Program to Merge Two Lists Using + or concatenate

In the below program, we have declared two integer lists of three elements in each one. In this programming language, the + operator will concatenate or merge two lists and produce a new list.

a = [10, 20, 30]
b = [40, 50, 60]

c = a + b

print(c)
[10, 20, 30, 40, 50, 60]

Using the list extend method

The built-in extend method will extend or expand the existing list by adding or merging another list. This Python program uses the extend() function to merge two integer lists.

a = [10, 20, 30]
b = [40, 50, 60]

a.extend(b)

print(a)
[10, 20, 30, 40, 50, 60]

Using * operator

In Python programming language, the * operator will unpack the list items. So, we used the * operator on both lists to unpack them and then converted them to a single list. To understand them better, we printed the unpacked a and b.

a = ["India", "USA", "China"]
b = ["UK", "Japan", "France", "Germany"]

print(*a)
print(*b)

c = [*a, *b]

print(c)
India USA China
UK Japan France Germany
['India', 'USA', 'China', 'UK', 'Japan', 'France', 'Germany']

Using itertools chain method

It is the fastest and best for the merge of largest lists. In this program, we import the itertools module and use the chain()function to merge two lists. The itertools.chain(a, b) line will create an iterator (<itertools.chain object>) with the items combination of a and b. Next, the list() function will convert the chain object to a list.

import itertools

a = [10, 20, 30]
b = [40, 50, 60, 70]

c = list(itertools.chain(a, b))

print(c)
[10, 20, 30, 40, 50, 60, 70]

Python Program to Merge Two Lists using for loop

Apart from the above-mentioned approaches, you can use the for loop to merge two lists. In the below program, we declare two string lists and the for loop will iterate the elements of the list (b). Within the loop, we used the append() function to add each value at the end of the list a.

a = ["India", "USA", "China"]
b = ["UK", "Japan", "France", "Germany"]

for val in b:
a.append(val)

print(a)
['India', 'USA', 'China', 'UK', 'Japan', 'France', 'Germany']

Instead of appending the b list to a the below program will declare a new list. Next, we used two for loops one after the other to traverse the list a and b. The append() function will those items to c.

a = ["India", "USA", "China"]
b = ["UK", "Japan", "France", "Germany"]

c = []

for val in a:
c.append(val)

for val in b:
c.append(val)

print(c)
Python Program to Merge Two List

Nested for loop

We have tweaked the above merge two lists example and used the nested for loop instead of multiple ones. The first for loop will traverse multiple lists in one go and the inner one will loop over the combined one.

a = [10, 20, 30]
b = [40, 50, 60, 70]

c = []

for val in (a, b):
for n in val:
c.append(n)

print(c)
[10, 20, 30, 40, 50, 60, 70]

Using a while loop

Instead of a For loop, this program uses the while loop and append() function to merge two lists.

a = [10, 20, 30]
b = [40, 50, 60]

i = 0

while i < len(b):
a.append(b[i])
i += 1

print(a)
[10, 20, 30, 40, 50, 60]

Using List comprehension 

There are multiple approaches that we can use while merging two lists using list comprehension. The first approach is using list comprehension to traverse each list and the + operator to merge them.

a = [10, 20, 30]
b = [40, 50, 60]

c = [val for val in a] + [val for val in b]

print(c)
[10, 20, 30, 40, 50, 60]

In the below program, we adjusted the code forgetter performance and more readable. Here, [num for num in (a, b)] returns [[10, 20, 30], [40, 50, 60, 70]]. So, we have used another for loop.

a = [10, 20, 30]
b = [40, 50, 60, 70]

c = [val for num in (a, b) for val in num]

print(c)

print([num for num in (a, b)])
[10, 20, 30, 40, 50, 60, 70]
[[10, 20, 30], [40, 50, 60, 70]]

Python Program to Merge Two Lists Using set()

If you want the unique items after merging the two lists, please use the set() function. When you are dealing with duplicates and looking for unique items from both lists, use the set method and + operator. As it returns a set we must the list() function to convert back.

a = [1, 2, 3, 4, 5, 7]
b = [4, 6, 7, 9]

c = a + b
print(c)

d = list(set(a + b))
print(d)
[1, 2, 3, 4, 5, 7, 4, 6, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 9]