Convert list of lists to flat list - flatten list in Python

How to convert list of lists to flat list – flatten list in Python ?

In this tutorial you will learn how to convert list of lists to flat list i.e., how to flatten the list of lists to single flat list using various approaches in Python programming language.

Convert list of lists to flat list – flatten list in Python

Convert list of lists to flat list - flatten list in PythonThere are many approaches using which you can convert list of lists to simple single flatten list. Let’s say the l is the given list of lists.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]

Nested for loops – flatten list

In the following example, we are using nested for loops to convert the given list of lists to single flat list.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#nested for loops
flat_list = []
for sublist in l:
for elem in sublist:
flat_list.append(elem)
#nested for loops flat_list = [] for sublist in l: for elem in sublist: flat_list.append(elem)
#nested for loops
flat_list = []
for sublist in l:
    for elem in sublist:
        flat_list.append(elem)

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>>> print(flat_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(flat_list) [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(flat_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

You can also write/ use “for” loops in the following way to achieve the same.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#forfor
flatten = [item for sublist in t for item in sublist]
print(flatten)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#forfor flatten = [item for sublist in t for item in sublist] print(flatten) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#forfor
flatten = [item for sublist in t for item in sublist]
print(flatten)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

using itertools

Here are some examples on how to use itertools chain to convert list of lists to single flat list in Python.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#itertools_chain
import itertools
flatten = list(itertools.chain(*l))
print(flatten)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#itertools_chain import itertools flatten = list(itertools.chain(*l)) print(flatten) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#itertools_chain
import itertools
flatten = list(itertools.chain(*l))
print(flatten)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

itertools.chain.from_iterable

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#itertools
import itertools
flatten = itertools.chain.from_iterable
print(list(flatten(l)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#itertools import itertools flatten = itertools.chain.from_iterable print(list(flatten(l))) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#itertools
import itertools
flatten = itertools.chain.from_iterable
print(list(flatten(l)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

functools.reduce() method to flatten list

In this example, we need to import two libraries: functools and operator and we will be using functools.reduce() method to convert list of lists to single list.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#functools_reduce
import functools
import operator
functools.reduce(operator.iconcat, l, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#functools_reduce import functools import operator functools.reduce(operator.iconcat, l, []) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#functools_reduce
import functools
import operator
functools.reduce(operator.iconcat, l, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

flat list using sum() method

Let’s see how to flatten list of lists to flat list using sum() method simply as shown below. This way you can sum lists and combine them to single list.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#sum of lists
print(sum(l, []))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#sum of lists print(sum(l, [])) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#sum of lists
print(sum(l, []))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Other ways to convert list of lists to flat list

You can also flatten list of lists to simple flat list using the following libraries: matplotlib, setuptools and numpy.

For example,

matplotlib

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#matplotlib
from matplotlib.cbook import flatten
print(list(flatten(l)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#matplotlib from matplotlib.cbook import flatten print(list(flatten(l))) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#matplotlib
from matplotlib.cbook import flatten
print(list(flatten(l)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

setuptools library

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#setuptools
from setuptools.namespaces import flatten
print(list(flatten(l)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#setuptools from setuptools.namespaces import flatten print(list(flatten(l))) [1, 2, 3, 4, 5, 6, 7, 8, 9]
#setuptools
from setuptools.namespaces import flatten
print(list(flatten(l)))    
[1, 2, 3, 4, 5, 6, 7, 8, 9]

using numpy

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
print (np.concatenate(l))
[1 2 3 4 5 6 7 8 9]
import numpy as np print (np.concatenate(l)) [1 2 3 4 5 6 7 8 9]
import numpy as np
print (np.concatenate(l))
[1 2 3 4 5 6 7 8 9]

That’s it. You have learnt numerous ways to flatten list of lists to flat list in Python.

Hope it is helpful 🙂

You’ll also like:

References

Subscribe
Notify of
guest


0 Comments
Inline Feedbacks
View all comments