By Emma


2009-06-04 20:30:05 8 Comments

I wonder whether there is a shortcut to make a simple list out of list of lists in Python.

I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with reduce, but I get an error.

Code

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

Error message

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'

30 comments

@Alex Martelli 2009-06-04 20:37:01

Given a list of lists l,

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

which means:

for sublist in l:
    for item in sublist:
        flat_list.append(item)

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

Here is the corresponding function:

flatten = lambda l: [item for sublist in l for item in sublist]

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

$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,l)'
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(L**2) when there are L 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 L sublists of I items each: the first I items are copied back and forth L-1 times, the second I items L-2 times, and so on; total number of copies is I times the sum of x for x from 1 to L excluded, i.e., I * (L**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.

@Kai 2009-06-04 20:41:51

Not sure if its the fastest but its the most readable solution to me since it doesn't rely on anything but list comprehensions.

@intuited 2010-10-15 01:21:33

I tried a test with the same data, using itertools.chain.from_iterable : $ python -mtimeit -s'from itertools import chain; l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'list(chain.from_iterable(l))'. It runs a bit more than twice as fast as the nested list comprehension that's the fastest of the alternatives shown here.

@Mike A 2011-03-16 18:47:23

$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'b = []' 'extend = b.extend' 'for sub in l:' ' extend(sub)' 10000 loops, best of 3: 36.6 usec per loop $ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]' 10000 loops, best of 3: 86.4 usec per loop

@Rob Crowell 2011-07-27 16:43:18

I found the syntax hard to understand until I realized you can think of it exactly like nested for loops. for sublist in l: for item in sublist: yield item

@Boris Chervenkov 2012-05-19 21:59:24

@intuited your solution actually returns an iterator, not an actual list - that's why it runs twice as fast. see type(itertools.chain([ [1,2], [3,4] ])). but if @emma needs the nested list just to iterate over it - that's a fine and optimal solution :o)

@intuited 2012-05-20 22:56:40

@BorisChervenkov: Notice that I wrapped the call in list() to realize the iterator into a list.

@Makoto 2012-07-19 08:04:55

numpy.concatenate seems a bit faster than any of the methods here, if you are willing to accept an array.

@Sven 2013-03-27 14:00:29

Doesn't universally work! l=[1,2,[3,4]] [item for sublist in l for item in sublist] TypeError: 'int' object is not iterable

@Mark E. Haase 2013-05-23 22:29:51

@Noio It makes sense if you re-order it: [item for item in sublist for sublist in l ]. Of course, if you re-order it, then it won't make sense to Python, because you're using sublist before you defined what it is.

@John Mee 2013-08-29 01:38:20

[leaf for tree in forest for leaf in tree] might be easier to comprehend and apply.

@Air 2013-10-03 17:51:46

@Sven It works for any list of lists; [1,2,[3,4]] is not a list of lists. You could hack together a one-line solution for that particular case with something like: [item for sublist in [sublist if isinstance(sublist, list) else [sublist] for sublist in l] for item in sublist]; but that's not concise enough to be worth fitting into one line. Alternatively, it's simple enough to write yourself a function for flattening arbitrarily nested sequences; see also stackoverflow.com/a/2158532/2359271

@Cruncher 2013-10-08 13:18:31

@wim I really just write the for loop structure out in full if i'm not sure what a list comprehension is doing. Though I will grant that at face value item for sublist in l for item in sublist sounds like nonsense.

@LBarret 2014-05-14 19:21:21

the reduce example in the main text use the + operator, and therefore create N list. reduce(lambda x, y : x.extend(y) or x, ll, []) is much faster than list comprehension in my test. The tricky part is the expression : x.extend(y) return None, so we use or to get the accumulator list extended : a or b return b if bool(a) evaluate to false.

@Joel 2015-01-04 12:55:36

@AlexMartelli Can you suggest whether this would still be the best option if all sublists are the same length (in my case length 2)?

@Alex Martelli 2015-01-04 15:40:56

@Joel, actually nowadays list(itertools.chain.from_iterable(l)) is best -- as noticed in other comments and Shawn's answer.

@Oren 2015-02-12 11:57:15

My attempt to understand [item**,** for sublist in l-> for item in sublist]

@Dan Lenski 2015-07-11 05:19:01

Really nice illustration! The versions based on sum/reduce fall victim to Schlemiel the Painter's algorithm, an antipattern named as such by Stack Overflow's founder Joel Spolsky :)

@JuanXarg 2015-08-26 13:06:14

Sorry to revive this very old thread, but I was curious why this solution is the best. If I'm reading the timeit output correctly, each loop of this solution runs roughly 10 times faster, but 10 times more loops are required.

@Alex Martelli 2015-08-28 21:50:27

@JuanXarg, nope, you're totally misunderstanding timeit's output: it iterates 10 more times because it can (within the rough constraint of taking about the same amount of elapsed time), not because the iterations are in any way, shape, or form, "required".

@Hamza Ouaghad 2016-06-10 18:13:36

Any sublist that is not actually a list but an str would get split too, anyway to dodge that in a pythonic way?

@Kenneth Jiang 2016-09-28 18:38:30

I don't care about how fast it runs as long as it's hard to understand!!! Can anyone tell me he/she can remember the syntax 3 months later without referring to this same post again? Can anyone tell me why you need to repeat item for sublist? Ugly Ugly Ugly!!

@ChaimG 2016-10-30 02:29:56

Why all the upvotes? reduce(operator.concat, list2d) is faster AND easier to understand! See this answer.

@Johannes Schaub - litb 2017-01-09 18:02:29

I have tried to put parentheses to clarify the associativity of these (nested?) generators.. but I fail so far. None of list(i for a in ([[1, 2], [3, 4]] for i in a)) and list((i for a in [[1, 2], [3, 4]]) for i in a) compile, but give syntax errors. I understand that the latest a refers to the first a introduced there. So I assumed that the expression must be left-associative. But the latter fails to parse aswell. Any hint to this?

@Arco Bast 2017-03-10 14:25:08

@KennethJiang I find it is extremely easy to memorize. Just use: [x for x in x for x in x]. It became one of my favorites :D

@Eric Duminil 2017-04-15 16:06:12

@JohnMee: [leaf for leaf in tree for tree in forest] would have been even better and much easier to read but sadly, Python devs didn't think so.

@MichaelChirico 2017-06-30 13:41:50

@intuited evaluating efficency on a list of 99 items hardly amounts to a true benchmark

@Davos 2017-09-12 03:42:25

At first the [leaf for tree in forest for leaf in tree] syntax seems confusing and in the wrong order, but its the same as a nested for loop, except the yield leaf is moved to the front so it fits the list comprehension standard. e.g. for tree in forest: for leaf in tree: yield leaf . The other suggested syntax order [leaf for leaf in tree for tree in forest] seems much more like piping or composition rather than nesting so yeah from that perspective it's easier to reason about that deep nesting.

@Davos 2017-09-12 03:51:50

I wish there were something like R's magrittr package for piping functions together in Python. It's generally easier to read from left to right rather than from inside out, although perhaps that's a very biased perspective from someone who natively writes a left-to-right language like English.

@Vineet 2018-04-19 03:49:48

@AlexMartelli, It doesn't work for [[1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9], [1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9], [1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9]]

@Deepak Yadav 2018-06-20 11:14:05

We can also do it in flat_list = [] for i in list_of_list: flat_list+=i

@Neal Gokli 2018-08-01 20:26:23

@AlexMartelli You said "nowadays list(itertools.chain.from_iterable(l)) is best". Can you add that to the top of your answer, with a link to a better answer? Your answer has so many upvotes that it seems authoritative!

@Esben Eickhardt 2019-01-22 08:46:36

Syntax in R: unlist(list_of_lists) Just saying ;)

@Triptych 2009-06-04 20:35:53

Note from the author: This is inefficient. But fun, because monoids are awesome. It's not appropriate for production Python code.

>>> sum(l, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

This just sums the elements of iterable passed in the first argument, treating second argument as the initial value of the sum (if not given, 0 is used instead and this case will give you an error).

Because you are summing nested lists, you actually get [1,3]+[2,4] as a result of sum([[1,3],[2,4]],[]), which is equal to [1,3,2,4].

Note that only works on lists of lists. For lists of lists of lists, you'll need another solution.

@Emma 2009-06-04 20:59:36

See Nadia's reply below, too. This appears to be the fastest solution.

@andrewrk 2010-06-15 18:55:14

that's pretty neat and clever but I wouldn't use it because it's confusing to read.

@Thomas Ahle 2011-05-20 17:31:07

@Nick: It's not immediately clear that you can do this. Even though + is overloaded, the sum function is implemented in C. I believe it has only recently been changed to support non number objects.

@Mike Graham 2012-04-25 18:24:57

This is a Shlemiel the painter's algorithm joelonsoftware.com/articles/fog0000000319.html -- unnecessarily inefficient as well as unnecessarily ugly.

@Mike Graham 2012-04-25 18:25:34

@ThomasAhle, I think you might be be confused.

@ulidtko 2014-12-03 10:35:23

The append operation on lists forms a Monoid, which is one of the most convenient abstractions for thinking of a + operation in a general sense (not limited to numbers only). So this answer deserves a +1 from me for (correct) treatment of lists as a monoid. The performance is concerning though...

@jhegedus 2015-10-05 08:51:42

@andrewrk Well, some people think that this is the cleanest way of doing it : youtube.com/watch?v=IOiZatlZtGU the ones who do not get why this is cool just need to wait a few decades until everybody does it this way :) let's use programming languages (and abstractions) that are discovered and not invented, Monoid is discovered.

@Jean-François Fabre 2017-07-31 18:04:59

this is a very inefficient way because of the quadratic aspect of the sum.

@ds4940 2018-01-04 16:46:19

This article explains the maths of the inefficiency mathieularose.com/how-not-to-flatten-a-list-of-lists-in-pyth‌​on

@isaaclw 2018-02-08 20:04:51

This doesn't seem to work in python 2.7, unless I'm doing something wrong. When I'm dealing with strings: TypeError: can only concatenate list (not "str") to list When I change it to ints, I get the same error but s/str/int/. I was assuming 'sum' is a standard operator... ?

@naught101 2018-02-20 01:28:46

So.. this is perfectly fine for short cases, then?

@J. Doe 2018-07-04 01:49:15

what is the use of this

@whackamadoodle3000 2018-07-18 12:12:39

@isaaclw then put a '' instead of a []

@Jean-François Fabre 2018-08-25 13:44:03

@ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 no, sum is protected against string summing. You have to use str.join

@mortonjt 2018-10-29 22:22:35

Note that this doesn't work in every situation. You may have to do sum(l+[[]]) instead

@Balamurugan 2018-12-17 07:23:55

you can use the chain class

chain.from_iterable(['ABC', 'DEF'])

output would be : [A B C D E F]

Reference : https://docs.python.org/2/library/itertools.html#itertools.chain

@Saurabh Singh 2018-12-14 10:51:18

Recursive version

x = [1,2,[3,4],[5,[6,[7]]],8,9,[10]]

def flatten_list(k):
    result = list()
    for i in k:
        if isinstance(i,list):

            #The isinstance() function checks if the object (first argument) is an 
            #instance or subclass of classinfo class (second argument)

            result.extend(flatten_list(i)) #Recursive call
        else:
            result.append(i)
    return result

flatten_list(x)
#result = [1,2,3,4,5,6,7,8,9,10]

@Nico Schlömer 2017-07-26 09:38:16

I tested most suggested solutions with perfplot (a pet project of mine, essentially a wrapper around timeit), and found

functools.reduce(operator.iconcat, a, [])

to be the fastest solution. (operator.iadd is equally fast.)

enter image description here


Code to reproduce the plot:

import functools
import itertools
import numpy
import operator
import perfplot


def forfor(a):
    return [item for sublist in a for item in sublist]


def sum_brackets(a):
    return sum(a, [])


def functools_reduce(a):
    return functools.reduce(operator.concat, a)


def functools_reduce_iconcat(a):
    return functools.reduce(operator.iconcat, a, [])


def itertools_chain(a):
    return list(itertools.chain.from_iterable(a))


def numpy_flat(a):
    return list(numpy.array(a).flat)


def numpy_concatenate(a):
    return list(numpy.concatenate(a))


perfplot.show(
    setup=lambda n: [list(range(10))] * n,
    kernels=[
        forfor, sum_brackets, functools_reduce, functools_reduce_iconcat,
        itertools_chain, numpy_flat, numpy_concatenate
        ],
    n_range=[2**k for k in range(16)],
    logx=True,
    logy=True,
    xlabel='num lists'
    )

@Sara 2019-01-20 13:57:20

For huge nested lists,' list(numpy.array(a).flat)' is the fastest among all functions above.

@Xero Smith 2018-11-28 01:00:34

This works with abitrarily nested lists. It can be easily extended to work with other kinds of iterables.

def flatten(seq):
    """list -> list                                                                                                                                                                           
    return a flattend list from an abitrarily nested list                                                                                                                                     
    """
    if not seq:
        return seq
    if not isinstance(seq[0], list):
        return [seq[0]] + flatten(seq[1:])
    return flatten(seq[0]) + flatten(seq[1:])

Sample run

>>> flatten([1, [2, 3], [[[4, 5, 6], 7], [[8]]], 9])
[1, 2, 3, 4, 5, 6, 7, 8, 9]

@user9074332 2018-09-24 02:08:16

The accepted answer did not work for me when dealing with text-based lists of variable lengths. Here is an alternate approach that did work for me.

l = ['aaa', 'bb', 'cccccc', ['xx', 'yyyyyyy']]

Accepted answer that did not work:

flat_list = [item for sublist in l for item in sublist]
print(flat_list)
['a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'xx', 'yyyyyyy']

New proposed solution that did work for me:

flat_list = []
_ = [flat_list.extend(item) if isinstance(item, list) else flat_list.append(item) for item in l if item]
print(flat_list)
['aaa', 'bb', 'cccccc', 'xx', 'yyyyyyy']

@Meitham 2016-09-14 15:09:16

There seems to be a confusion with operator.add! When you add two lists together, the correct term for that is concat, not add. operator.concat is what you need to use.

If you're thinking functional, it is as easy as this::

>>> list2d = ((1, 2, 3), (4, 5, 6), (7,), (8, 9))
>>> reduce(operator.concat, list2d)
(1, 2, 3, 4, 5, 6, 7, 8, 9)

You see reduce respects the sequence type, so when you supply a tuple, you get back a tuple. let's try with a list::

>>> list2d = [[1, 2, 3],[4, 5, 6], [7], [8, 9]]
>>> reduce(operator.concat, list2d)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Aha, you get back a list.

How about performance::

>>> list2d = [[1, 2, 3],[4, 5, 6], [7], [8, 9]]
>>> %timeit list(itertools.chain.from_iterable(list2d))
1000000 loops, best of 3: 1.36 µs per loop

from_iterable is pretty fast! But it's no comparison to reduce with concat.

>>> list2d = ((1, 2, 3),(4, 5, 6), (7,), (8, 9))
>>> %timeit reduce(operator.concat, list2d)
1000000 loops, best of 3: 492 ns per loop

@Mr_and_Mrs_D 2017-05-28 13:20:00

Hmm to be fair second example should be list also (or first tuple ?)

@hash_purple 2018-09-20 04:55:19

A simple recursive method using reduce from functools and the add operator on lists:

>>> from functools import reduce
>>> from operator import add
>>> flatten = lambda lst: [lst] if type(lst) is int else reduce(add, [flatten(ele) for ele in lst])
>>> flatten(l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

The function flatten takes in lst as parameter. It loops all the elements of lst until reaching integers (can also change int to float, str, etc. for other data types), which are added to the return value of the outermost recursion.

Recursion, unlike methods like for loops and monads, is that it is a general solution not limited by the list depth. For example, a list with depth of 5 can be flattened the same way as l:

>>> l2 = [[3, [1, 2], [[[6], 5], 4, 0], 7, [[8]], [9, 10]]]
>>> flatten(l2)
[3, 1, 2, 6, 5, 4, 0, 7, 8, 9, 10]

@tharndt 2018-01-09 14:34:15

Another unusual approach that works for hetero- and homogeneous lists of integers:

from typing import List


def flatten(l: list) -> List[int]:
    """Flatten an arbitrary deep nested list of lists of integers.

    Examples:
        >>> flatten([1, 2, [1, [10]]])
        [1, 2, 1, 10]

    Args:
        l: Union[l, Union[int, List[int]]

    Returns:
        Flatted list of integer
    """
    return [int(i.strip('[ ]')) for i in str(l).split(',')]

@Darkonaut 2018-01-10 22:03:32

That's just a more complicated and a bit slower way of what ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 already posted before. I reinvented his proposal yesterday, so this approach seems quite popular these days ;)

@tharndt 2018-01-11 08:17:10

Not quite: wierd_list = [[1, 2, 3], [4, 5, 6], [7], [8, 9], 10] >> nice_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0]

@tharndt 2018-01-11 08:32:18

my code as one liner would be : flat_list = [int(e.replace('[','').replace(']','')) for e in str(deep_list).split(',')]

@Darkonaut 2018-01-11 16:31:09

You are indeed right +1, ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000's proposal won't work with multiple digit numbers, I also didn't test this before although it should be obvious. You could simplify your code and write [int(e.strip('[ ]')) for e in str(deep_list).split(',')]. But I'd suggest to stick with Deleet's proposal for real use cases. It doesn't contain hacky type transformations, it's faster and more versatile because it naturally also handles lists with mixed types.

@tharndt 2018-01-13 08:02:22

Thanks! This was of course supposed to be funny. I've seen Deleet's proposal in a python book before.

@Darkonaut 2018-01-13 16:04:15

Can you tell us which book? I contemplated a lot about this because it's so effective and beautiful. Will hit recursion limit inevitably in general but for cases like this with few recursions it seems perfect.

@tharndt 2018-01-15 08:18:01

Unfortunately no. But I saw this code recently here: Python Practice Book 6.1.2

@pylang 2016-11-29 04:14:45

Here is a general approach that applies to numbers, strings, nested lists and mixed containers.

Code

from collections import Iterable


def flatten(items):
    """Yield items from any nested iterable; see Reference."""
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            for sub_x in flatten(x):
                yield sub_x
        else:
            yield x

Note: in Python 3, yield from flatten(x) can replace for sub_x in flatten(x): yield sub_x

Demo

lst = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
list(flatten(lst))                                         # nested lists
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

mixed = [[1, [2]], (3, 4, {5, 6}, 7), 8, "9"]              # numbers, strs, nested & mixed
list(flatten(mixed))
# [1, 2, 3, 4, 5, 6, 7, 8, '9']

Reference

  • This solution is modified from a recipe in Beazley, D. and B. Jones. Recipe 4.14, Python Cookbook 3rd Ed., O'Reilly Media Inc. Sebastopol, CA: 2013.
  • Found an earlier SO post, possibly the original demonstration.

@Martin Thoma 2017-03-25 15:32:05

I just wrote pretty much the same, because I didn't see your solution ... here is what I looked for "recursively flatten complete multiple lists" ... (+1)

@pylang 2017-03-25 17:51:51

@MartinThoma Much appreciated. FYI, if flattening nested iterables is a common practice for you, there are some third-party packages that handle this well. This may save from reinventing the wheel. I've mentioned more_itertools among others discussed in this post. Cheers.

@Triptych 2017-04-14 21:30:22

Nice - was just wondering about a yield from type of construction on python after learning about yield * in es2015.

@Jorge Leitão 2017-05-13 23:40:43

replace by if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)): to support strings.

@Wolf 2017-06-15 10:22:27

Maybe traverse could also be a good name for this way of a tree, whereas I'd keep it less universal for this answer by sticking to nested lists.

@Ryan Allen 2018-04-30 16:46:07

You can check if hasattr(x, '__iter__') instead of importing/checking against Iterable and that will exclude strings as well.

@Dims 2018-11-12 18:51:13

Excessive for...

@A. Attia 2018-07-24 09:11:26

You can use numpy :
flat_list = list(np.concatenate(list_of_list))

@InAFlash 2018-08-16 11:51:30

This will work only for numerical data

@Nitin 2018-09-19 07:53:11

This works for numerical, strings and mixed lists also

@EL_DON 2018-02-01 18:22:49

matplotlib.cbook.flatten() will work for nested lists even if they nest more deeply than the example.

import matplotlib
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
print(list(matplotlib.cbook.flatten(l)))
l2 = [[1, 2, 3], [4, 5, 6], [7], [8, [9, 10, [11, 12, [13]]]]]
print list(matplotlib.cbook.flatten(l2))

Result:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

This is 18x faster than underscore._.flatten:

Average time over 1000 trials of matplotlib.cbook.flatten: 2.55e-05 sec
Average time over 1000 trials of underscore._.flatten: 4.63e-04 sec
(time for underscore._)/(time for matplotlib.cbook) = 18.1233394636

@pylang 2016-12-02 18:35:17

Consider installing the more_itertools package.

> pip install more_itertools

It ships with an implementation for flatten (source, from the itertools recipes):

import more_itertools


lst = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
list(more_itertools.flatten(lst))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

As of version 2.4, you can flatten more complicated, nested iterables with more_itertools.collapse (source, contributed by abarnet).

lst = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
list(more_itertools.collapse(lst)) 
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

lst = [[1, 2, 3], [[4, 5, 6]], [[[7]]], 8, 9]              # complex nesting
list(more_itertools.collapse(lst))
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

@abhi krishnan 2018-07-02 05:56:24

We can do the same in, Using the basic concepts of python

nested_list=[10,20,[30,40,[50]],[80,[10,[20]],90],60]
flat_list=[]
def unpack(list1):
    for item in list1:
        try:
            len(item)
            unpack(item)
        except:
            flat_list.append(item)
unpack(nested_list)
print (flat_list)

@Deepak Yadav 2018-06-20 11:12:25

flat_list = []
for i in list_of_list:
    flat_list+=i

This Code also works fine as it just extend the list all the way. Although it is much similar but only have one for loop. So It have less complexity than adding 2 for loops.

@Imesha Sudasingha 2018-06-20 11:17:25

It is already described in this answer

@Deepak Yadav 2018-06-20 11:30:16

@ImeshaSudasingha The answer have used 2 for loops instead I am using only one

@Brad Solomon 2018-02-01 18:33:22

Note: Below applies to Python 3.3+ because it uses yield_from. six is also a third-party package, though it is stable. Alternately, you could use sys.version.


In the case of obj = [[1, 2,], [3, 4], [5, 6]], all of the solutions here are good, including list comprehension and itertools.chain.from_iterable.

However, consider this slightly more complex case:

>>> obj = [[1, 2, 3], [4, 5], 6, 'abc', [7], [8, [9, 10]]]

There are several problems here:

  • One element, 6, is just a scalar; it's not iterable, so the above routes will fail here.
  • One element, 'abc', is technically iterable (all strs are). However, reading between the lines a bit, you don't want to treat it as such--you want to treat it as a single element.
  • The final element, [8, [9, 10]] is itself a nested iterable. Basic list comprehension and chain.from_iterable only extract "1 level down."

You can remedy this as follows:

>>> from collections import Iterable
>>> from six import string_types

>>> def flatten(obj):
...     for i in obj:
...         if isinstance(i, Iterable) and not isinstance(i, string_types):
...             yield from flatten(i)
...         else:
...             yield i


>>> list(flatten(obj))
[1, 2, 3, 4, 5, 6, 'abc', 7, 8, 9, 10]

Here, you check that the sub-element (1) is iterable with Iterable, an ABC from itertools, but also want to ensure that (2) the element is not "string-like."

@pylang 2018-06-19 19:06:43

If you are still interested in Python 2 compatibility, change yield from to a for loop, e.g. for x in flatten(i): yield x

@Paritosh Yadav 2018-06-14 05:17:46

Use list comprehension:

mylist = [item for sublist in l for item in sublist]

@David 2018-06-14 05:41:07

Welcome to Stack Overflow! Would be nice if you could edit your answer to be a bit more clear. Thanks!

@TooYoung 2018-06-14 14:38:43

This answer is duplicated with other earlier answers

@phoxis 2018-05-16 09:41:57

This may not be the most efficient way but I thought to put a one-liner (actually a two-liner). Both versions will work on arbitrary hierarchy nested lists, and exploits language features (Python3.5) and recursion.

def make_list_flat (l):
    flist = []
    flist.extend ([l]) if (type (l) is not list) else [flist.extend (make_list_flat (e)) for e in l]
    return flist

a = [[1, 2], [[[[3, 4, 5], 6]]], 7, [8, [9, [10, 11], 12, [13, 14, [15, [[16, 17], 18]]]]]]
flist = make_list_flat(a)
print (flist)

The output is

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

This works in a depth first manner. The recursion goes down until it finds a non-list element, then extends the local variable flist and then rolls back it to the parent. Whenever flist is returned, it is extended to the parent's flist in the list comprehension. Therefore, at the root, a flat list is returned.

The above one creates several local lists and returns them which are used to extend the parent's list. I think the way around for this may be creating a gloabl flist, like below.

a = [[1, 2], [[[[3, 4, 5], 6]]], 7, [8, [9, [10, 11], 12, [13, 14, [15, [[16, 17], 18]]]]]]
flist = []
def make_list_flat (l):
    flist.extend ([l]) if (type (l) is not list) else [make_list_flat (e) for e in l]

make_list_flat(a)
print (flist)

The output is again

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

Although I am not sure at this time about the efficiency.

@MSeifert 2016-11-26 00:20:37

If you want to flatten a data-structure where you don't know how deep it's nested you could use iteration_utilities.deepflatten1

>>> from iteration_utilities import deepflatten

>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> list(deepflatten(l, depth=1))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> l = [[1, 2, 3], [4, [5, 6]], 7, [8, 9]]
>>> list(deepflatten(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

It's a generator so you need to cast the result to a list or explicitly iterate over it.


To flatten only one level and if each of the items is itself iterable you can also use iteration_utilities.flatten which itself is just a thin wrapper around itertools.chain.from_iterable:

>>> from iteration_utilities import flatten
>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> list(flatten(l))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Just to add some timings (based on Nico Schlömer answer that didn't include the function presented in this answer):

enter image description here

It's a log-log plot to accommodate for the huge range of values spanned. For qualitative reasoning: Lower is better.

The results show that if the iterable contains only a few inner iterables then sum will be fastest, however for long iterables only the itertools.chain.from_iterable, iteration_utilities.deepflatten or the nested comprehension have reasonable performance with itertools.chain.from_iterable being the fastest (as already noticed by Nico Schlömer).

from itertools import chain
from functools import reduce
from collections import Iterable  # or from collections.abc import Iterable
import operator
from iteration_utilities import deepflatten

def nested_list_comprehension(lsts):
    return [item for sublist in lsts for item in sublist]

def itertools_chain_from_iterable(lsts):
    return list(chain.from_iterable(lsts))

def pythons_sum(lsts):
    return sum(lsts, [])

def reduce_add(lsts):
    return reduce(lambda x, y: x + y, lsts)

def pylangs_flatten(lsts):
    return list(flatten(lsts))

def flatten(items):
    """Yield items from any nested iterable; see REF."""
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            yield from flatten(x)
        else:
            yield x

def reduce_concat(lsts):
    return reduce(operator.concat, lsts)

def iteration_utilities_deepflatten(lsts):
    return list(deepflatten(lsts, depth=1))


from simple_benchmark import benchmark

b = benchmark(
    [nested_list_comprehension, itertools_chain_from_iterable, pythons_sum, reduce_add,
     pylangs_flatten, reduce_concat, iteration_utilities_deepflatten],
    arguments={2**i: [[0]*5]*(2**i) for i in range(1, 13)},
    argument_name='number of inner lists'
)

b.plot()

1 Disclaimer: I'm the author of that library

@Yann Vernier 2018-05-14 06:29:00

sum no longer works on arbitrary sequences as it starts with 0, making functools.reduce(operator.add, sequences) the replacement (aren't we glad they removed reduce from builtins?). When the types are known it might be faster to use type.__add__.

@MSeifert 2018-05-15 09:24:44

@YannVernier Thanks for the information. I thought I ran these benchmarks on Python 3.6 and it worked with sum. Do you happen to know on which Python versions it stopped working?

@Yann Vernier 2018-05-15 09:31:10

I was somewhat mistaken. 0 is just the default starting value, so it works if one uses the start argument to start with an empty list... but it still special cases strings and tells me to use join. It's implementing foldl instead of foldl1. The same issue pops up in 2.7.

@ASz 2018-03-16 18:38:13

I think the easiest way and universal is to write recursive function like this:

def flat_list(some_list = []):
elements=[]
for item in some_list:
    if type(item) == type([]):
        elements += flat_list(item)
    else:
        elements.append(item)
return elements

list =  ['a', 'b', 1, 2, 3, [1, 2, 3, 'c',[112,123,111,[1234,1111,3333,44444]]]]
flat_list(list)

>>> ['a', 'b', 1, 2, 3, 1, 2, 3, 'c', 112, 123, 111, 1234, 1111, 3333, 44444]

@bli 2018-01-31 09:52:50

This can be done using toolz.concat or cytoolz.concat (cythonized version, that could be faster in some cases):

from cytoolz import concat
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
list(concat(l)) # or just `concat(l)` if one only wants to iterate over the items

On my computer, in python 3.6, this seems to time almost as fast as [item for sublist in l for item in sublist] (not counting the import time):

In [611]: %timeit L = [item for sublist in l for item in sublist]
695 ns ± 2.75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [612]: %timeit L = [item for sublist in l for item in sublist]
701 ns ± 5.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [613]: %timeit L = list(concat(l))
719 ns ± 12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [614]: %timeit L = list(concat(l))
719 ns ± 22.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

The toolz version is indeed slower:

In [618]: from toolz import concat

In [619]: %timeit L = list(concat(l))
845 ns ± 29 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [620]: %timeit L = list(concat(l))
833 ns ± 8.73 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

@Greg Hewgill 2009-06-04 20:35:30

from functools import reduce #python 3

>>> l = [[1,2,3],[4,5,6], [7], [8,9]]
>>> reduce(lambda x,y: x+y,l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

The extend() method in your example modifies x instead of returning a useful value (which reduce() expects).

A faster way to do the reduce version would be

>>> import operator
>>> l = [[1,2,3],[4,5,6], [7], [8,9]]
>>> reduce(operator.concat, l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

@agf 2011-09-24 10:04:39

reduce(operator.add, l) would be the correct way to do the reduce version. Built-ins are faster than lambdas.

@lukmdo 2012-03-20 22:13:11

@agf here is how: * timeit.timeit('reduce(operator.add, l)', 'import operator; l=[[1, 2, 3], [4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7]]', number=10000) 0.017956018447875977 * timeit.timeit('reduce(lambda x, y: x+y, l)', 'import operator; l=[[1, 2, 3], [4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7]]', number=10000) 0.025218963623046875

@Mike Graham 2012-04-25 18:26:07

This is a Shlemiel the painter's algorithm joelonsoftware.com/articles/fog0000000319.html

@Freddy 2015-09-11 07:16:54

this can use only for integers. But what if list contains string?

@Greg Hewgill 2015-09-11 07:38:59

@Freddy: The operator.add function works equally well for both lists of integers and lists of strings.

@Vineet 2018-04-19 03:45:13

It doesn't work for [[1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9], [1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9], [1, 2, 3], [4, 5, 6], [2, 3, 4], [3, 4, 5], [4, 6, 7], [5, 7, 9]]

@Rupen B 2019-02-10 02:31:49

I tried reduce with operator.concat on a list of 1,000,000 integer sublists of lengths 1-10, and it never returned (after 1 minute). The list comprehension, operator.itertools and a simple list.extend over loop all returned in sub-seconds. The last method was the fastest!

@Chweng Mega 2017-12-12 11:42:35

Isn't it the most obvious solution😂
fList = eval('[' + str(l).replace('[','').replace(']','') + ']') # Just for fun

@FredMan 2017-10-19 05:46:09

You can avoid recursive calls to the stack using an actual stack data structure pretty simply.

alist = [1,[1,2],[1,2,[4,5,6],3, "33"]]
newlist = []

while len(alist) > 0 :
  templist = alist.pop()
  if type(templist) == type(list()) :
    while len(templist) > 0 :
      temp = templist.pop()
      if type(temp) == type(list()) :
        for x in temp :
          templist.append(x)
      else :
        newlist.append(temp)
  else :
    newlist.append(templist)
print(list(reversed(newlist)))

@Some Java Programmer 2018-04-18 14:05:44

This doesn't support iterable collections other than lists. You might want to consider using isinstance(temp, Iterable) like some of the other examples. I think you can also simplify this a bit, if you add alist to templist at the beginning, you should only need the nested while loop. You could also use a queue data structure in order to avoid reversing the entire list at the end.

@Jon 2017-09-21 18:53:02

I recently came across a situation where I had a mix of strings and numeric data in sublists such as

test = ['591212948',
['special', 'assoc', 'of', 'Chicago', 'Jon', 'Doe'],
['Jon'],
['Doe'],
['fl'],
92001,
555555555,
'hello',
['hello2', 'a'],
'b',
['hello33', ['z', 'w'], 'b']]

where methods like flat_list = [item for sublist in test for item in sublist] have not worked. So, I came up with the following solution for 1+ level of sublists

def concatList(data):
    results = []
    for rec in data:
        if type(rec) == list:
            results += rec
            results = concatList(results)
        else:
            results.append(rec)
    return results

And the result

In [38]: concatList(test)
Out[38]:
 Out[60]:
['591212948',
'special',
'assoc',
'of',
'Chicago',
'Jon',
'Doe',
'Jon',
'Doe',
'fl',
92001,
555555555,
'hello',
'hello2',
'a',
'b',
'hello33',
'z',
'w',
'b']

@englealuze 2017-08-08 14:59:07

def flatten(alist):
    if alist == []:
        return []
    elif type(alist) is not list:
        return [alist]
    else:
        return flatten(alist[0]) + flatten(alist[1:])

@devil in the detail 2017-07-05 05:14:38

Following seem 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]

@stason 2018-06-23 20:51:53

doesn't work with already flat arrays, consider: import numpy as np l = [1, 2, 3] print (np.concatenate(l)) ValueError: zero-dimensional arrays cannot be concatenated

@stason 2018-06-25 16:58:16

I'm not sure what you achieve by downvoting a comment that identifies a flaw in your answer. The voting system is here to help others save their time using answers that work. Yours works only partially as indicated above and makes an assumption on the input being non-flat and doesn't generalize.

@Vu Anh 2017-03-25 05:09:57

Simple code for underscore.py package fan

from underscore import _
_.flatten([[1, 2, 3], [4, 5, 6], [7], [8, 9]])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

It solves all flatten problems (none list item or complex nesting)

from underscore import _
# 1 is none list item
# [2, [3]] is complex nesting
_.flatten([1, [2, [3]], [4, 5, 6], [7], [8, 9]])
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

You can install underscore.py with pip

pip install underscore.py

@gliemezis 2017-06-06 03:22:40

Similarly, you can use pydash. I find this version to be much more readable than the list comprehension or any other answers.

@Nico Schlömer 2017-07-26 09:52:16

This is super slow.

@EL_DON 2018-07-20 18:04:35

Why does it have a module named _? That seems like a bad name. See stackoverflow.com/a/5893946/6605826

@Vu Anh 2018-07-21 02:26:20

@EL_DON: From underscore.py readme page "Underscore.py is a python port of excellent javascript library underscore.js". I think it's the reason for this name. And yes, It's not a good name for python

@Kevin Postal 2017-03-22 17:01:57

Cleaned up @Deleet example

from collections import Iterable

def flatten(l, a=[]):
    for i in l:
        if isinstance(i, Iterable):
            flatten(i, a)
        else:
            a.append(i)
    return a

daList = [[1,4],[5,6],[23,22,234,2],[2], [ [[1,2],[1,2]],[[11,2],[11,22]] ] ]

print(flatten(daList))

Example: https://repl.it/G8mb/0

@pylang 2017-03-25 18:22:52

Thanks for the approach. I have one comment on your keyword a. Mutable default args have side effects and are often avoided in Python. The link has a solution for this. Cheers.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How to clone or copy a list?

25 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 2961499 View
  • 3259 Score
  • 25 Answer
  • Tags:   python directory

6 Answered Questions

[SOLVED] How to get the number of elements in a list in Python?

  • 2009-11-11 00:30:54
  • y2k
  • 2949635 View
  • 1722 Score
  • 6 Answer
  • Tags:   python list

36 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2061718 View
  • 3083 Score
  • 36 Answer
  • Tags:   python list

40 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

26 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3086310 View
  • 2544 Score
  • 28 Answer
  • Tags:   python list

30 Answered Questions

[SOLVED] How to concatenate two lists in Python?

16 Answered Questions

[SOLVED] How to make a chain of function decorators?

57 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

Sponsored Content