How to flatten a list of lists in Python
In Python, there is no direct method or function to flatten a list of lists into a single list. This tutorial shows how to do it.
Create a list of lists
For our example, we will use a list of lists prices
and a multinested list of lists historical_prices
.
prices = [[3.5, 2.4], [4, 5, 6], [8.1, 9.2, 7]]
historical_prices = [[3.5, 2.4], [4, 5, [7, 7.2, 7.4, 7.8], 6], [8.1, [6, 6.1, 6.2, 6.3, [2.1, 2.2, 2.3]], 9.2, 7]]
We will create a second variable all_prices
that contains the flattened list.
Flatten a list of lists using for loop with extend()
This code loops through the outer list, grabs all the items in the inner list and adds them to the new list using the .extend()
method.
prices = [[3.5, 2.4], [4, 5, 6], [8.1, 9.2, 7]]
all_prices = []
for pricelist in prices:
all_prices.extend(pricelist)
print(all_prices)
Output:
[3.5, 2.4, 4, 5, 6, 8.1, 9.2, 7]
Flatten a multinested list of lists using for loop with extend()
This code loops through the outer list, grabs all the items in the inner list, checks if any items are lists themselves, extracts them, and adds them to the new list using the .extend()
method. Since there may be several levels of multinested lists, a recursive solution is efficient.
historical_prices = [[3.5, 2.4], [4, 5, [7, 7.2, 7.4, 7.8], 6], [8.1, [6, 6.1, 6.2, 6.3, [2.1, 2.2, 2.3]], 9.2, 7]]
all_prices = []
def flatten_nested_list(prices):
for price in prices:
if type(price) == list:
flatten_nested_list(price)
else:
all_prices.append(price)
flatten_nested_list(historical_prices)
print(all_prices)
Output:
[3.5, 2.4, 4, 5, 7, 7.2, 7.4, 7.8, 6, 8.1, 6, 6.1, 6.2, 6.3, 2.1, 2.2, 2.3, 9.2, 7]
Flatten a list of lists using functools.reduce()
The functools
module a function reduce()
that implements a functional programming paradigm. The reduce()
function takes two arguments - an input function and an iterable. The input function is applied on the next iterable element with the result from the previous run. This results in an output which is cumulative.
from functools import reduce
prices = [[3.5, 2.4], [4, 5, 6], [8.1, 9.2, 7]]
all_prices = list(reduce(lambda x, y: x + y, prices, []))
print(all_prices)
Output:
[3.5, 2.4, 4, 5, 6, 8.1, 9.2, 7]
Flatten a list of lists using itertools.chain()
The itertools
module has a function chain()
that returns an iterator with a flattened list. We convert that iterator to a list by enclosing it in a list()
function.
from itertools import chain
prices = [[3.5, 2.4], [4, 5, 6], [8.1, 9.2, 7]]
all_prices = list(chain(*prices))
print(all_prices)
Output:
[3.5, 2.4, 4, 5, 6, 8.1, 9.2, 7]
Flatten a list of lists using NumPy
If you have a NumPy array that contains nested arrays ,and want to flatten it, the function .flatten()
will do the job.
This method is very useful if you are working on machine learning projects and want a very efficient way of converting multidimensional lists or arrays into a single array or list.
import numpy as np
prices = [[3.5, 2.4], [4, 5, 6], [8.1, 9.2, 7]]
np_prices = num.array(prices)
np_prices = np.flatten()
print(np_prices)
Conclusion
These are only a few methods of flattening a list of lists in Python. There are other ways of doing the same thing. Depending on your situation, you can use the appropriate method.
If you found this blog post useful, feel free to share it. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.