Skip to content
BytePatterns

Flatten a Nested List

EasyRecursion#recursion#tree-walk~15m

Problem

You are given a list whose items are either integers or further lists, nested to any depth. Return a single flat list holding every integer in the order they appear when reading the structure left to right. Empty lists contribute nothing.

Examples

Input:  items = [1, [2, [3, 4]], 5]
Output: [1, 2, 3, 4, 5]
Why:    depth does not change the reading order
Input:  items = [[], [[]], [1]]
Output: [1]
Why:    empty lists at any depth disappear
Input:  items = []
Output: []
Why:    edge case, nothing to read at all

Hints

0 / 3

Stuck on the idea rather than the code? Recursion Basics covers it.