Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do make a flat list out of list of lists in Python?
A nested list is a list that contains other lists as elements. For example: [[1,2,3], [4,5,6], [7,8,9]] is a nested list with 3 sublists as its elements.
To flatten a list of lists (convert a 2D list into a 1D list), Python offers several approaches: nested for loops, list comprehension, built-in functions like itertools.chain(), or using libraries like NumPy.
In this article, we will explore these methods to flatten a list in Python.
Using Nested For Loops
The most straightforward approach is to iterate through each sublist and add its elements to a new flat list ?
def flatten_list(nested_list):
flat_list = []
for sublist in nested_list:
if isinstance(sublist, list):
for item in sublist:
flat_list.append(item)
else:
flat_list.append(sublist)
return flat_list
nested_data = [[11, 12, 13], [14, 15, 16, 17], [18, 19]]
result = flatten_list(nested_data)
print('Original List:', nested_data)
print('Flattened List:', result)
Original List: [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattened List: [11, 12, 13, 14, 15, 16, 17, 18, 19]
Using List Comprehension
List comprehension provides a more concise and Pythonic way to flatten nested lists ?
nested_data = [[11, 12, 13], [14, 15, 16, 17], [18, 19]]
flat_list = [item for sublist in nested_data for item in sublist]
print('Original List:', nested_data)
print('Flattened List:', flat_list)
Original List: [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattened List: [11, 12, 13, 14, 15, 16, 17, 18, 19]
Using itertools.chain()
The itertools.chain() function efficiently flattens iterables by chaining them together ?
import itertools
nested_data = [[11, 12, 13], [14, 15, 16, 17], [18, 19]]
flat_list = list(itertools.chain.from_iterable(nested_data))
print('Original List:', nested_data)
print('Flattened List:', flat_list)
Original List: [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattened List: [11, 12, 13, 14, 15, 16, 17, 18, 19]
Using NumPy
NumPy provides array manipulation functions that can flatten nested structures ?
import numpy as np
nested_data = [[11, 12, 13], [14, 15, 16, 17], [18, 19]]
flat_list = list(np.concatenate(nested_data))
print('Original List:', nested_data)
print('Flattened List:', flat_list)
Original List: [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattened List: [11, 12, 13, 14, 15, 16, 17, 18, 19]
Comparison
| Method | Performance | Best For |
|---|---|---|
| Nested For Loops | Good | Complex logic, mixed types |
| List Comprehension | Fast | Simple cases, readable code |
| itertools.chain() | Very Fast | Large datasets, memory efficiency |
| NumPy | Excellent | Numerical data, scientific computing |
Conclusion
Use list comprehension for simple cases and readable code. For large datasets or performance-critical applications, itertools.chain() is the most efficient choice. NumPy works best with numerical data in scientific computing contexts.
