Python: Flattening lists

Flattening a list refers to the procedure of unpacking nested lists this list. This means, if we have a list [“a”, [“ba”, “bb”], “c”], then the flattened version of this list would look like: [“a”, “ba”, “bb”, “c”]. The flatten operation can be implemented in several different ways.

All of the following code snippets have been tested with Python 3.3.

Our running example is the following list:

list_of_lists = ["a", ["ba", "bb"], ["ca"]]

itertools.chain

The least verbose one is to use chain from the itertools module (since 2.6):

import itertools
# works on a number of lists that are given as separate parameters
l1 = list(itertools.chain(*list_of_lists))
# works on a single "2D" list
l2 = list(itertools.chain.from_iterable(list_of_lists))

Clearly, this should be the preferred way of implementing flatten in Python, because that is what it actually does – in one, short line.

reduce

Compared to this, the following code looks rather hacky. Note that reduce was a built-in function in earlier versions of Python.

import functools
l3 = functools.reduce(lambda x,y: x + y if isinstance(y, list) else x + [y], list_of_lists, [])

sum

If all elements of list_of_list were lists, then the abbreviated form of this is

l4 = sum([["a"], ["ba", "bb"], ["ca"]],[])

Running times

There are some quite fine-grained timing analyses available in the Stackoverflow questions that deal with this question, for instance by cdleary.

 

References

  • [1] (one out of many) stackoverflow question about this problem

Leave a Reply