runtime
317 / 500
question 317 Iterators & Generators · medium

Flatten nested lists

Tag lists from different sources arrive nested to different depths: ["python", ["data", ["pandas", "numpy"]], ("web",)]. Write flatten(value): return a flat list of every item that isn't a list or tuple, in order, however deep the nesting. Strings and dictionaries are items, not containers.

Functionflatten(value)
valuea list or tuple whose items may be lists or tuples again
Returnsa new list of the non-container items, depth first, in order
Examples
callflatten(['python', ['data', ['pandas', 'numpy']], ['web']])returns['python', 'data', 'pandas', 'numpy', 'web']
callflatten([1, [2, [3, [4, [5]]]]])returns[1, 2, 3, 4, 5]
callflatten([])returns[]

Loading your progress…

Flatten nested lists — Python practice · Runtime