How to make a flat list out of a list of lists

Asked : Nov 17

Viewed : 25 times

Is there a shortcut to make a simple list out of a list of lists in Python?

I can do it in a for loop, but is there some cool "one-liner"?

I tried it with functools.reduce():

from functools import reduce
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
reduce(lambda x, y: x.extend(y), l)

But I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'
python list multidimensional-array flatten 
4 Answers

Given a list of lists t,

flat_list = [item for sublist in t for item in sublist]

which means:

flat_list = []
for sublist in t:
    for item in sublist:
        flat_list.append(item)

is faster than the shortcuts posted so far. (t is the list to flatten.)

Here is the corresponding function:

def flatten(t):
    return [item for sublist in t for item in sublist]

As evidence, you can use the timeit module in the standard library:

$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in t for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(t, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s't=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,t)'
1000 loops, best of 3: 1.1 msec per loop

Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(T**2) when there are T sublists -- as the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated, and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the end). So, for simplicity and without actual loss of generality, say you have T sublists of k items each: the first k items are copied back and forth T-1 times, the second k items T-2 times, and so on; the total number of copies is k times the sum of x for x from 1 to T excluded, i.e., k * (T**2)/2.

The list comprehension just generates one list, once, and copies each item over (from its original place of residence to the result list) also exactly once.

answered Jan 20


To make a flat list out of a list of lists with Python, we can use the sum function.

For instance, we write:

sublist = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
flat_list = sum(sublist, [])
print(flat_list)

We call sum with sublist and an empty array to put all the entries of the nested arrays into the empty array and return it.

Therefore flat_list is [1, 2, 3, 4, 5, 6, 7, 8, 9].

answered Jan 20


The following seems simplest to me:

>>> import numpy as np
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> print(np.concatenate(l))
[1 2 3 4 5 6 7 8 9]

answered Jan 20


The most common way to solve the problem is by using loops. I think most of you already got it. Let’s see the steps to solve the problem using loops.

  • Initialize the list of lists with dummy data and name it as data.
  • Now, initialize an empty list called flat_list.
  • Iterate over the data.
    • Unpack all the elements from the current list.
    • Add them to the flat_list using the list append method.
  • Print the result.

See the code for the problem below.

# initializing the data and an empty list
data = [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]
flat_list = []

# iterating over the data
for item in data:
    # appending elements to the flat_list
    flat_list += item

# printing the resultantn flat_list
print(flat_list)

You can use another loop to add sub-list elements to flat_list instead of a concatenation operator. We can also use list comprehensions instead of loops.

Both do the same work. Let’s see the next way to solve the problem.

answered Jan 20


Login and Submit Your Answer
Browse other questions tagged  python  list  multidimensional-array  flatten or ask your own question.