Merge Lists In Python

In our previous article we have seen that python lists are one of the most widely used data structures in python for various data processing operations. Also we introduce you to an exciting list technique called List Comprehension. In this article we will be covering different method to merging lists.
1. Using +
operator
Using the +
operator we can create a new list with elements from both the list.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = list1 + list2
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
2. Using extend()
Method
The extend()
method appends all elements from one list to another. This updates the first list by adding the elements of the second list to its end.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
# Extend list1 with elements from list2
list1.extend(list2)
print(list1) # Output: [1, 2, 3, "a", "b", "c"]
3. Using *
Operator
The
operator (Unpacking) to unpack the elements of multiple lists and combine them into a new list.*
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = [*list1, *list2]
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
4. Using List Comprehension
List comprehension
can also be used to merge lists. In our article on list comprehension, we explored its capabilities and demonstrated how it works.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = [*list1, *list2]
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
5. Using for loop
Lists can be merged using a simple for
loop.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = []
for item in list1:
merged_list.append(item)
for item in list2:
merged_list.append(item)
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
6. Using sum()
Method
You can merge multiple lists by using the sum()
function.
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list= sum([list1, list2], [])
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
7. Using itertools.chain()
The chain()
method from the itertools
module is used to combine multiple iterables. This is suitable when we are dealing with a large datasets.
from itertools import chain
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = list(chain(list1, list2))
print(merged_list) # Output: [1, 2, 3, "a", "b", "c"]
8. Using zip() function
We can use zip()
when we need to merge lists pairwise (or in an interleaved manner).
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
merged_list = [item for pair in zip(list1, list2) for item in pair]
print(merged_list) # Output: [1, "a", 2, "b", 3, "c"]
Conclusion
In this blog, we’ve covered various ways to merge lists in Python, including the +
operator, extend()
method, list comprehension, and advanced techniques like itertools.chain()
and zip()
. The +
operator and extend()
are simple for basic merges, while itertools.chain()
and unpacking with *
are more efficient for larger or more complex tasks. It is important to know these methods. They help you choose the best one for your needs. This ensures your code is clean, efficient, and easy to read.