How to flatten a list of lists in Python
Tags: django, python, flat
Published: 2021-06-22 05:41:58

---
title: How to flatten a list of lists in Python
published: true
date: 2020-12-21 07:31:16 UTC
tags: Django,Python,django,flat
canonical_url: https://vuyisile.com/how-to-flatten-a-list-of-lists-in-python/
---

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:

```python

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

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

```

### 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`:

```python

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]

```

### 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:

```python

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]

```

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

```python

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

```

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:

```python

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]

```

## Other ways to flatten lists

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

### Reduce

```python

>>> from functools import reduce

>>> import operator

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

```

### Itertools

```python

>>> import itertools

>>> flat_list - itertools.chain_from_iterable(list1)

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

```

### Matplotlib

```python

>>> from matplotlib.cbook import flatten

>>> list(flatten(list1)

```

### Pandas

```python

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

```

### Setuptools

```python

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

```

### Django

```python

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

```

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](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) article for timings.

**References:**

[1) Stackoverflow](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists)