---
title: "Comment by Ruud van der Ham on \"7 Different Ways to Flatten a List of Lists in Python\" | Hashnode"
description: "Here&#39;s a different, non recursive, solution for deep flattening: def flatten ( bad ): good = [] while bad: e = bad.pop() if isinstance(e, list): bad.extend("
url: "https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdbceceb78ac62dff80a5c9"
---

[Discussion](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7)

Comment [7 Different Ways to Flatten a List of Lists in Python](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7)

[Ruud van der Ham](https://hashnode.com/@ruudvanderham)

[Dec 17, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdbceceb78ac62dff80a5c9)

Here's a different, non recursive, solution for deep flattening:

```
def flatten(bad):
    good  = []
    while bad:
        e = bad.pop()
        if isinstance(e, list):
            bad.extend(e)
        else:
            good.append(e)
    return good[::-1]
```

Very likely quite fast as well.

[Hynek Černoch](https://hashnode.com/@hynekcer)

[Dec 18, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdbceceb78ac62dff80a5c9#reply-5fdc35ddb78ac62dff80aac7)

This require a `copy.deepcopy()` to prevent a side effects because the original input parameter is completely broken by this function.

[Ruud van der Ham](https://hashnode.com/@ruudvanderham)

[Dec 18, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdbceceb78ac62dff80a5c9#reply-5fdc4a7bb78ac62dff80ac65)

[Hynek Černoch](https://hashnode.com/@hynekcer) Good point. It is true that the original list will be destroyed. But if that doen't matter a deep copy is not required.

[Ruud van der Ham](https://hashnode.com/@ruudvanderham)

[Dec 18, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdbceceb78ac62dff80a5c9#reply-5fdcdfc91485f22e24b33d31)

[Hynek Černoch](https://hashnode.com/@hynekcer) Maybe it would be better to use this quite different function as an inline flatten, very much like list.sort() or list.remove(). The return value is then always None and the list will be replaced by the flattened version. That can be implemented like:

```
def flatten_inplace(lst):
    build  = []
    while lst:
        e = lst.pop()
        if isinstance(e, list):
            lst.extend(e)
        else:
            build.append(e)
    lst.extend(build[::-1])
```
