Deviopath › Python

Flatten Any Nesting Depth

Medium · Recursion

A recursive function calls itself on a smaller piece of the problem. For nested lists the rule is simple: if an item is a list, flatten it (recursively) and add its items; otherwise add the item itself. Use isinstance(item, list) to check.

Write deep_flatten(items) that returns a flat list of all non-list values, in order, no matter how deeply the lists are nested. Strings are values, not lists.

Next: Merge Two Sorted Lists