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:

itertools.chain

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

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.

sum

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

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

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

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box