How to Flatten List of Lists in Python

Sometimes Python developers need to flatten a list of lists. This means they need to convert a nested two-dimensional list into a single one-dimensional list. It simplifies data and makes it easier to process. There are several ways to do this in Python. In this article, we will learn the different ways to flatten list in Python.

What is meant by Flatten List in Python

Lists are versatile data structures used by many Python developers. A list can contain numbers, texts and even other lists as individual items. When you have a list where each item itself is a list, then it is called nested list or list of lists. It can be difficult to work with list of lists. Often Python developers need to convert this into a single list by copying the individual items of original list to a new list. This is called flattening of list in Python. Let us say you have the following list of lists.

data = [[1, 2],[3, 4],[5, 6]]

Let us say you want to flatten the above list. In this case, you will obtain the following result.

new_data = [1, 2, 3, 4, 5, 6]

As you can see above, the result is a single list with all items from original list present.

How to Flatten List of Lists in Python

Here are the different ways to flatten list of lists in Python.

1. Using For Loop

One of the most common methods is to simply loop through the list of lists and in each iteration, we append the inner list’s item to the resultant flat list. Here is a code to demonstrate it.

>>> new_data = []
>>> for i in data:
for j in i:
new_data.append(j)

>>> new_data
[1, 2, 3, 4, 5, 6]

Every list supports append() function that allows you to append a new item to the list. We use it to copy elements from original list to new list.

In the above case, we need to run two loops, one within the other. Alternatively, you can iterate over the original list and in each iteration, use extend() function to easily append all items of each row to the new list.

>>> new_data = []
>>> for i in data:
new_data.extend(i)

>>> new_data
[1, 2, 3, 4, 5, 6]

In the above case, you need only one loop. So it is much faster than using append() function earlier.

You can also loop through the list of lists and in each iteration, use concatenation operator (+), to append the list item to new flattened list.

>>> new_data=[]
>>> for i in data:
new_data+=i

>>> new_data
[1, 2, 3, 4, 5, 6]

This method is quite straightforward, easy to understand and can be customized as per your requirement. For example, you can choose to exclude certain items from the final list, by adding if…else condition inside for loops.

2. Using itertools.chain

Itertools is a popular & efficient Python module that provides many useful functions that use iterators to quickly loop through data. In other words, it provides functions to loop through large data with minimal memory at fast pace.

You can use itertools.chain() or itertools.chain.from_iterable() function to flatten a list. Let us look at both of them.

>>> import itertools
>>> new_data=[]
>>> new_data=list(itertools.chain(*data))
>>> new_data
[1, 2, 3, 4, 5, 6]

In the above code, we simply provide the original list of lists, preceded by unpacking operator (*) in itertools.chain(). It will do the rest and flatten the list.

If you use itertools.chain.from_iterable(), then you do not even need to provide the unpacking operator.

>>> import itertools
>>> new_data=[]
>>> new_data=list(itertools.chain.from_iterable(data))
>>> new_data
[1, 2, 3, 4, 5, 6]

You can use itertools.chain() or itertools.chain.from_iterable() for small as well as large list of lists. Since it uses iterators, it does not load the data completely in memory. Instead, it just uses the iterators to the list, which hardly takes up any space. Also, it is significantly faster than other methods and highly scalable.

3. Using List Comprehensions

List comprehensions are one line expressions or statements that allow you to easily perform a lot of operations on a list. They are basically similar to loops but much faster. They can be used to flatten a list of lists. Here is their basic syntax.

[expression(item) for item in iterable]

Here is an example to flatten our list.

>>> new_data=[]
>>> new_data=[item for row in data for item in row]
>>> new_data
[1, 2, 3, 4, 5, 6]

In the above list comprehension, there are 2 for loops. The first one iterates over each list(row) in the original list. The second one iterates over each item in the row.

Although it looks a little complicated, it is much faster than using sum() or reduce() functions.

4. Using Numpy.concatenate

Numpy is a popular Python library for data analysis. It provides many functions to work with numbers. If your list of lists consists of only numbers, then you can use numpy.concatenate() function to flatten such as list.

>>> import numpy as np

>>> new_data = np.concatenate(data).tolist()
>>> new_data
[1, 2, 3, 4, 5, 6]

In the above code, we also need to call tolist() function to convert the output of numpy.concatenate() to a list.

5. Using sum()

You can also use sum() function to flatten a list. It will begin with an empty list and add the individual lists of list of lists, into it, one by one. So it is important to pass an empty list as the second argument of sum() function.

>>> new_data=[]
>>> new_data=sum(data,[])
>>> new_data
[1, 2, 3, 4, 5, 6]

It is not as fast as other methods listed above but it is super simple and easy to understand. It works well for small list of lists. You can also pass a non-empty list as second argument, if you want to not only flatten your list but concatenate it with another list.

>>> new_data=sum(data,[10])

>>> new_data
[10, 1, 2, 3, 4, 5, 6]

So it is a very convenient shortcut to easily flatten and concatenate lists or list or lists. But please note, if you want to flatten & concatenate two list of lists, you need to use two sum() functions, one for each list of lists, as shown below.

>>> data = [[1,2],[3,4],[5,6]]
>>> data1 = [[10,11],[12,13],[14,15]]
>>> new_data = sum(data,sum(data1,[]))
>>> new_data
[10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6]

In the above code, we call sum() function twice. First, to flatten second list, and then use it to flatten the first list.

6. Using functools.reduce

Python also provides functools library for functional programming. Among them, reduce() is a powerful function that offers many uses. It takes a couple of arguments, performs desired tasks, computes and stores partial result. Then it iterates to the next item, and uses it along with previous result to perform another computation. It once again stores the result. It continues this process till end of items. It is very useful for flattening a list in Python.

>>> data=[[1,2],[3,4],[5,6]]
>>> new_data = reduce(lambda x,y:x+y,data)
>>> new_data
[1, 2, 3, 4, 5, 6]

In the above code, we use lambda function in reduce(). Lambda functions are small, anonymous functions that contain just a single statement or expression. It takes couple of arguments x, y and concatenates them using + operator. The first argument is a lambda function. The second argument concatenates 2 items and stores the result in y. The third argument is the data list of lists.

So basically, the lambda function starts with an empty list. It takes the first 2 items of the list of lists, and concatenates them. In other words, it joins the first 2 rows to create a single list. Then it concatenates this list with the next item of the list, that is, the third row of the list of lists to create the final result.

7. Using operator

In the above solution, we have manually defined the lambda function to concatenate list of lists. Python also provides operator library that contains many handy functions for efficiently performing certain operations in Python. You can use them with reduce() function to easily concatenate list of lists. Here is an example that uses add(), concat() and iconcat() operator functions which can be directly invoked on any data item.

>>> from operator import add, concat, iconcat
>>> from functools import reduce

>>> new_data=reduce(add,data)
>>> new_data
[1, 2, 3, 4, 5, 6]

>>> new_data=reduce(concat,data)
>>> new_data
[1, 2, 3, 4, 5, 6]

>>> new_data=reduce(iconcat,data)
>>> new_data
[1, 2, 3, 4, 5, 6]

Conclusion

In this article, we have learnt many different ways to flatten list of lists in Python. You can use any of these methods depending on your requirement. Among them, using itertools and list comprehensions provide the fastest ways to flatten a list of lists. Numpy.concatenate() function works with only list of lists containing numbers. All other methods are suitable for all data types.

FAQ

1. Which is the fastest way to flatten list of lists?

Using itertools or list comprehensions provide the fastest ways to flatten list of lists.

2. Do these solutions work for all data types?

Using numpy.concatenate() works for only numeric data types. All other solutions work for all data types.

Also read:

How to Access Index Value in For Loop in Python
How to Create Python Directory With Missing Parent
Does Python Have Ternary Operator?

Leave a Reply

Your email address will not be published. Required fields are marked *