Files
30-seconds-of-code/snippets/deep_flatten.md
2020-01-03 13:08:53 +02:00

22 lines
486 B
Markdown

---
title: deep_flatten
tags: list,recursion,intermediate
---
Deep flattens a list.
Use recursion.
Use `isinstance()` with `collections.abc.Iterable` to check if an element is iterable.
If it is, apply `deep_flatten()` recursively, otherwise return `[lst]`.
```py
from collections.abc import Iterable
def deep_flatten(lst):
return [a for i in lst for a in deep_flatten(i)] if isinstance(lst, Iterable) else [lst]
```
```py
deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5]
```