In this article we will discuss different ways to Merge / Join two or more lists in python.

Table of Contents

Suppose we have two lists i.e.

# List of strings
list1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list2 = [10, 2, 45, 3, 5, 7, 8, 10]

We want to merge the contents of these two list into a single list i.e.

['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

There are different ways to do this. Let’s discuss them by one by one.

Join / Merge two lists in python using + operator

In python, we can use the + operator to merge the contents of two lists into a new list. For example,

We can use + operator to merge two lists i.e.

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Merge two lists
final_list = list_1 + list_2

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It returned a new concatenated lists, which contains the contents of both list_1 and list_2. Whereas, list_1 and list_2 remained same as original.

Join / Merge two lists in python using list.extend()

In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend() function of list i.e.

list.extend(anotherList)

list.extend() makes a list longer by appending the elements of another list at the end of the calling list object. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Makes list1 longer by appending the elements of list2 at the end.
list_1.extend(list_2)

print('Merged List:')
print(list_1)

Output

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It extended the list_1 by adding the contents of list_2 at the end of list_1.

Join / Merge two lists in python using unpacking

In python, we can unpack the contents on any iterable object using the * operator. So, *list will unpack the contents of a list. We can unpack the contents of both the lists and create a new list with the merged contents. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Merge two lists
final_list = [*list_1, *list_2]

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It unpacked the contents of both the lists and created a new list with the contents of both the lists.

Join / Merge two lists in python using itertools.chain()

In python, the itertools module provides a function chain() to merge the contents of multiple iterable sequences,

itertools.chain(*iterables)

It creates a chain of all the iterable sequences passed as arguments and returns an iterator.

This iterator returns the elements from first iterable sequence until it is exhausted and then moves to the next iterable. We can use this iterator to created a merged list of contents. For example,

import itertools

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Join two lists
final_list=list(itertools.chain(list_1, list_2))

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Join / Merge two lists in python using for loop

We can iterate over all elements of a list using for loop and during iteration we can append each element to an another list. This way we can extend the contents of a list. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]


# Iterate over a list and add elements to another list
for elem in list_2:
    list_1.append(elem)

print('Extended List:')
print(list_1)

Output:

Extended List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

We iterated over all the elements in list_2 and while iteration added each element at the end of list_1. Therefore list_1 is now extended and contains the contents of both the lists i.e. original list_1 and list_2.

Join / Merge multiple lists using + operator

We can merge the contents of multiple lists to a new list using the + operator. For example,

list_1 = ["This" , "is", "a", "sample", "program"]
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]
list_3 = [11, 12, 13]

# Merge 3 lists into a single list    
merged_list = list_1 + list_2 + list_3

print('Merged List:')
print(merged_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

Conclusion:

We learned about different ways to join or merge multiple lists in python.