How to Flatten a List in Python

As we often explain, the list is one of the most used data types in Python. Sometimes, we can have what is called a two-dimension list, also referred to as nested lists or list of lists. For example:

[[1,2,3],[4,5,6],[7,8,9]]
Code language: Python (python)

This structure of often used for Pandas DataFrame where each list represents a row. That being said, when we want to count frequencies, detect duplicates etc… this nested structure is not very handy, and we need to flatten (or concatenate) it, which means transforming a nested list into a regular one. Using our example below, we’d end up with:

[1,2,3,4,5,6,7,8,9]
Code language: JSON / JSON with Comments (json)

In this tutorial, we’ll see the different options we ha ve at our disposal to achieve this transformation.

Use a list comprehension to flatten a list

One of the simplest ways of flattening a list is to use a list comprehension. It works with regular and irregular list of lists.

  • Regular: each list has the same number of elements
  • Irregular: each list can have a different number of elements
original_list = [[1,2,3], [4,5], [6, 7, 8, 9]] flattened_list = [element for list in my_list for element in list] print(flattened_list)
Code language: Python (python)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Code language: Python (python)

This approach is great, but note that it doesn’t remove duplicates by default. If you want to keep unique values using a list comprehension, you will have to modify a little bit the behavior of your list comprehension.

my_list = [[1,2,3], [1,2,3], [6, 7, 8, 9]] flattened_list = [] [flattened_list.append(element) for list in my_list for element in list if element not in flattened_list ] print(flattened_list)
Code language: Python (python)

Output:

[1, 2, 3, 6, 7, 8, 9]
Code language: Python (python)

Use a for loop to flatten a list

If you’re not familiar with list comprehension, you can also create a custom function using a regular for loop to achieve the same objective. I’d rather use a function in this case because:

  • You’re likely to use it more than once
  • It’s longer to write a full for loop than a list comprehension. hence, to reduce the number of useless line of codes, we can use a function here when it was necessary for a list comprehension
my_list = [[1,2,3], [1,2,3], [6, 7, 8, 9]] def flatten_list(list_to_flatten): flattened_list = [] for element in list_to_flatten: for item in element: flattened_list.append(item) return flattened_list flattened_list = flatten_list(my_list) print(flattened_list)
Code language: Python (python)

Output:

[1, 2, 3, 1, 2, 3, 6, 7, 8, 9]
Code language: Python (python)

Use itertools to flatten a nested list

We can use the itertools library to flatten our list. If we have a look at the definition of the chain() function, we can understand why it is a good option:

def chain(*iterables): # chain('ABC', 'DEF') --> A B C D E F for it in iterables: for element in it: yield element
Code language: PHP (php)

This function treats lists as a single list by iterating through them.

import itertools my_list = [[1, 2, 3], [4, 5, 6], [7, 8]] flattened_list = list(itertools.chain(*my_list)) print(flattened_list)
Code language: Python (python)

Output:

[1, 2, 3, 4, 5, 6, 7, 8]
Code language: JSON / JSON with Comments (json)

Awesome, right? There are two drawbacks, though :

  • You need to import the itertools library, which can create a dependency you want to avoid
  • You have to unpack the output of the function. This is why we used the list() method in the third line of our code
import itertools my_list = [[1, 2, 3], [4, 5, 6], [7, 8]] flattened_list = list(itertools.chain(*my_list)) print(flattened_list)
Code language: Python (python)

Use sum() function to flatten a list

The sum() function is known to return the sum of float or int values inside a list. But you can tweak it to achieve this flattening operation.

If we look at the definition of this function, we have a hint: Sums start and the items of an iterable from left to right and returns the total. The iterable’s items are normally numbers, and the start value is not allowed to be a string.

Given that the sum of two lists combine them:

print([1,2]+[3,4])
Code language: Python (python)

Output:

[1, 2, 3, 4]
Code language: Python (python)

We can understand how we can tweak this function to return what we want:

sum([[1, 2], [3, 4]], []) == [] + [1, 2] + [3, 4] == [1,2,3,4]
Code language: Python (python)

Amazing, right? It is not the most obvious way to achieve our transformation, but it works like a charm.

Use reduce() to flatten a list of lists

Reduce() is a built-in function, which takes a set of values (a list, a tuple, or any iterable object) as an argument and “reduces” it to a single value. How that single value is obtained from the collection passed as an argument will depend on the function applied.

As you can see, it is pretty simple:

from functools import reduce def add(a, b): return a + b print(reduce(add, [[1,2],[3,4]]))
Code language: Python (python)

You can also use a lambda function to avoid creating a custom function in this case:

from functools import reduce def add(a, b): return a + b print(reduce(lambda x,y:x+y, [[1,2],[3,4]]))
Code language: JavaScript (javascript)

Output:

[1, 2, 3, 4]
Code language: Python (python)

Use Pandas to flatten a list

Finally, you can leverage the explode() method from the Pandas library. I recommend doing it like only if you are already working with a DataFrame.

import pandas as pd df = {'strings':[['A','B','C'],['D','E','F']]} df = pd.DataFrame(df, columns=['strings']) print(df.explode('strings'))
Code language: JavaScript (javascript)

Output:

A
B
C
D
E
F

Leave a Reply

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