In Python, you often encounter scenarios where you have a list of lists, and you need to merge them into a single list. This is common in data processing, flattening nested structures, and working with multi-dimensional lists.
This guide will walk you through different methods to join a list of lists in Python efficiently.
1. Using List Comprehension (Fast and Pythonic)
List comprehension is a concise and efficient way to flatten a list of lists.
Example
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = [item for sublist in list_of_lists for item in sublist]
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
This method iterates through each sublist and extracts individual elements into a new list.
2. Using itertools.chain
(Memory Efficient)
The itertools
module provides chain()
which efficiently flattens a list of lists without creating intermediate lists.
Example
import itertools
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = list(itertools.chain(*list_of_lists))
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
This method is more memory efficient than list comprehension as it avoids creating unnecessary temporary lists.
3. Using sum()
with an Empty List (Simple but Slow)
A quick way to merge lists is by using the sum()
function with an empty list as the starting value.
Example
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = sum(list_of_lists, [])
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
Why Avoid This Method?
Although this approach looks elegant, it is inefficient. Python’s sum()
performs repeated concatenation, leading to high time complexity (O(n^2)
). It should be avoided for large datasets.
4. Using functools.reduce
(Not Recommended)
Another approach is using functools.reduce()
with list concatenation.
Example
from functools import reduce
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = reduce(lambda x, y: x + y, list_of_lists)
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
Why Avoid This Method?
Like sum()
, this method is inefficient because +
concatenates lists repeatedly, leading to poor performance (O(n^2)
). It should only be used for very small lists.
5. Using numpy.concatenate
(For NumPy Users)
If you are working with NumPy arrays, numpy.concatenate()
is a great option.
Example
import numpy as np
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = np.concatenate(list_of_lists).tolist()
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
This method is particularly useful when working with numerical data in NumPy.
6. Using extend()
in a Loop (Efficient and Readable)
Another efficient way is using a loop and extend()
, which avoids excessive list creation.
Example
list_of_lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
flattened_list = []
for sublist in list_of_lists:
flattened_list.extend(sublist)
print(flattened_list)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
This method is both efficient (O(n)
) and easy to understand.
Which Method Should You Use?
Method | Performance | Memory Efficiency | Readability |
---|---|---|---|
List Comprehension | ✅ Fast (O(n) ) | ✅ Efficient | ✅ Readable |
itertools.chain | ✅ Fast (O(n) ) | ✅ Efficient | ✅ Readable |
sum([], list_of_lists) | ❌ Slow (O(n^2) ) | ❌ Inefficient | ✅ Readable |
functools.reduce | ❌ Slow (O(n^2) ) | ❌ Inefficient | ❌ Less Readable |
numpy.concatenate | ✅ Fast (O(n) ) | ✅ Efficient (for NumPy) | ❌ Less Readable |
extend() in loop | ✅ Fast (O(n) ) | ✅ Efficient | ✅ Readable |
Conclusion
If you need to join a list of lists in Python:
- ✅ Use list comprehension for a simple and efficient solution.
- ✅ Use
itertools.chain()
for a memory-efficient approach. - ✅ Use
extend()
in a loop for readability and efficiency.
Avoid using sum()
and functools.reduce()
as they perform poorly on large lists. If you work with NumPy, numpy.concatenate()
is a great option.
By choosing the right method, you can improve your code’s efficiency and readability.
- Roblox Force Trello - February 25, 2025
- 20 Best Unblocked Games in 2025 - February 25, 2025
- How to Use Java Records to Model Immutable Data - February 20, 2025