DEV Community

loading...

How to flatten a list of lists in Python

Vuyisile Ndlovu
Software Developer Interested in backend development, automation, Python and Django.
Originally published at vuyisile.com on ・2 min read

In this article I will go over a couple of ways to flatten lists of lists in Python.

Let’s assume you have the following lists:


list1 =  [[1, 2], [3, 4], [5, 6, 7], [8] ]

list2 = [1, [2, 3], [4, 5], 6, [7, 8]]

Enter fullscreen mode Exit fullscreen mode

Flattening a list of lists

list1 is a proper list of lists while list2 is a list that contains lists and non-lists.

To flatten list1:


def flatten1(l):
    result = []
    for item in l:
        for subitem in item:
            result.append(subitem)
    return result

>>> flat_list = flatten1(list1)
>>> flat_list
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

Flattening a list containing lists and other types

The code above uses a two for loops to iterate through each list element and each sub-list. On each iteration of the sub-list, it appends the list element to an empty list. The code above can be also be written using a generator this way:


def flatten1(l):
    for item in l:
        for subitem in item:
            yield subitem

>>> flat_list = list(flatten1(list1))
>>> flat_list
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

Alternatively, you can make it concise by using a list comprehension:


>>> flat_list = [subitem for item in l for subitem in item]
>>> flat_list
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

The examples above work very well for lists of lists. If some list elements are not lists, as in the case in list2, you’ll want to modify your list flattening function a little:


def flatten1(l):
    for item in l:
        if not isinstance(item, list):
            yield item
        else:
            for subitem in item:
                yield subitem

>>> flat_list = flatten1(list2)
>>> flat_list
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

Other ways to flatten lists

Below is a list of additional ways to flatten lists depending on the tool you prefer.

Reduce


>>> from functools import reduce

>>> import operator

>>> reduce(operator.add, list1)
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

Itertools


>>> import itertools

>>> flat_list - itertools.chain_from_iterable(list1)

>>> list(flat_list)
[1, 2, 3, 4, 5, 6, 7, 8]

Enter fullscreen mode Exit fullscreen mode

Matplotlib


>>> from matplotlib.cbook import flatten

>>> list(flatten(list1)

Enter fullscreen mode Exit fullscreen mode

Pandas


>>> from pandas.core.common import flatten
>>> list(flatten(list1))

Enter fullscreen mode Exit fullscreen mode

Setuptools


>>> from setuptools.namespaces import flatten
>>> list(flatten(list1))

Enter fullscreen mode Exit fullscreen mode

Django


>>> from django.contrib.admin.utils import flatten

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

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

Enter fullscreen mode Exit fullscreen mode

Django’s flatten function can flatten any type of list.

Conclusion

You’ve seen how to flatten lists of lists as well as flattening lists that contain mixed data. This article showed you different methods of achieving the same result using Pandas, Django, itertools, Matplotlib, Reduce and the good old for loop. If you would like to know more about the performance of each of these methods, go through the comments in this stackoverflow article for timings.

References:

1) Stackoverflow

Discussion (0)