Open In App

Python - Merge elements of sublists

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, we often need to combine multiple sublists into a single list. This can be useful when dealing with data that is organized in smaller chunks, like rows in a table or pages in a document.

Using itertools.chain()

itertools.chain() function is one of the most efficient ways to merge sublists. It works by combining multiple iterable (like lists) into a single iterable. The best part is that it doesn’t create intermediate lists, making it very memory-efficient. The *a unpacks the list of sublists (a), so each sublist is passed as a separate argument to itertools.chain().

Python
import itertools

# Given list of sublists
a = [[1, 2], [3, 4], [5, 6]]

# Merge using itertools.chain
b = list(itertools.chain(*a))

# Print the output
print(b)

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

Other methods that we can use to merge sublist in python are:

Using extend() with Loop

Another method to merge sublists is by using a loop along with the extend() method. The extend() method allows us to add multiple elements from one list into another list. This method works similarly to list comprehension but is more explicit and can be useful if you prefer using loops.

Python
a = [[1, 2], [3, 4], [5, 6]]

# Initialize an empty list
b = []

# Loop through each sublist and extend b with its elements
for sublist in a:
    b.extend(sublist)

print(b)

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

Using sum() Function

Although the sum() function is mostly used to add numbers, it can also be used to merge sublists. It works by "adding" all the sublists together, starting from an empty list. However, it's not the most efficient method, especially for large datasets, because it creates new lists during the process.

Python
a = [[1, 2], [3, 4], [5, 6]]

# Merge using sum
b = sum(a, [])

print(b)

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

Using functools.reduce()

reduce() function from the functools module applies a function repeatedly to combine elements of an iterable. In this case, we can use it to merge sublists. This method is a bit more advanced. reduce() applies the lambda function to combine two lists at a time. The lambda x, y: x + y function takes two lists (x and y) and concatenates them.

Python
from functools import reduce

# Given list of sublists
a = [[1, 2], [3, 4], [5, 6]]

# Merge using reduce
b = reduce(lambda x, y: x + y, a)

# Print the output
print(b)

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

Using Nested Loop with append()

nested loop with append() is the most manual way to merge sublists. This method gives us full control over the merging process. It's useful for beginners who are just learning about loops and lists. We create an empty list b. Then, we loop through each sublist in a. For each sublist, we loop through its items and append each item to b using append().

Python
# Given list of sublists
a = [[1, 2], [3, 4], [5, 6]]

# Initialize an empty list
b = []

# Loop through each sublist and append each item to b
for sublist in a:
    for item in sublist:
        b.append(item)

# Print the output
print(b)

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

Using numpy.concatenate() (for Numeric Data)

If we're dealing with numerical data and have numpy installed, we can use numpy.concatenate() to merge sublists. It’s highly efficient for large arrays of numbers and works well when you're already using numpy in your project. numpy.concatenate() takes multiple arrays (or sublists) and combines them into one.

Python
import numpy as np

# Given list of sublists
a = [[1, 2], [3, 4], [5, 6]]

# Merge using numpy.concatenate
b = np.concatenate(a)

# Print the output
print(b)

Output
[1 2 3 4 5 6]

Similar Reads