How to flatten a list of lists in Python
Know how vanilla Python and a few libraries can be used to flatten a list of lists.
A flat list is a type of list which is not nested, for example:
#more
["h", "e", "l", "l", "o"]
[True, 1, 2, False]
And nested lists:
[[7], [0, 9, 3], [4, 6, 8]]
[["lorem", "ipsum", "seth", "sir"], ["domat", "texeto", "do"]]
There are several ways in which a nested list can be unpacked and made into a flat list, some of these approaches don’t need a library whereas others use itertools, functools, and numpy.
1. Looping and List comprehension¶
This is the easiest way to flatten a list. It uses a for loop to iterate over the main list and another nested for loop to iterate over the element of the main list.
nested_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = []
for sublist in nested_list:
for element in sublist:
flat_list.append(element)
print(flat_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
A shorter version of this uses list comprehension:
flat_list = [element for sublist in nested_list for element in sublist]
Alternatively, extend() can be used to create the resulting list without nested loops
flat_list = []
for sublist in nested_list:
flat_list.extend(element)
2. itertools.chain(*nested_list)¶
Itertools is part of python’s standard libraries and provides a method to create a flat list. The chain method takes lists as arguments, therefore a *
is used to unpack the list, read more about *args here, the return value is an iterator and not a list, using list() it is forced to yield all elements.
import itertools
nested_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = itertools.chain(*nested_list)
flat_list = list(flat_list)
3. itertools.chain.from_iterable(nested_list)¶
Similar to itertools.chain() but takes a nested list as argument.
import itertools
nested_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = itertools.chain.from_iterable(nested_list)
flat_list = list(flat_list)
4. functools.reduce(function, nested_list)¶
reduce() works by applying a function to two elements of an iterable cumulatively.
from functools import reduce
nested_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = reduce(lambda x, y: x+y, nested_list)
Alternatively, instead of writing lambda function, in-built operator.concat can be used.
import operator
from functools import reduce
nested_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = reduce(operator.concat, nested_list)
5. numpy.concatenate(nested_list)¶
Returns merged list instead of an iterator
import numpy
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat_list = numpy.concatenate(nested_list)
6. numpy.array(nested_list).flat¶
Numpy arrays have a flat property which can be used to get an iterator for a flattened array but it only works if lists inside a nested list are of same lengths.
import numpy
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(list(numpy.array(nested_list).flat))
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
If sub lists are not of same length
import numpy
nested_list = [[1, 2, 3], [4, 5], [7, 8, 9]]
print(list(numpy.array(nested_list).flat))
Output:
[[1, 2, 3], [4, 5], [7, 8, 9]]
FREE VS Code / PyCharm Extensions I Use
✅ Write cleaner code with Sourcery, instant refactoring suggestions: Link*
Python Problem-Solving Bootcamp
🚀 Solve 42 programming puzzles over the course of 21 days: Link*