easy +10 pts

Flatten Multidimensional Lists

Recursively flatten a deeply nested list of integers into a flat list.

Write a function `flatten_list(nested)` that takes a list `nested` which may contain integers and/or other lists (to any depth) and returns a flat list containing all the integers in the order they appear. For example, `flatten_list([1, [2, 3], [[4]], 5])` should return `[1, 2, 3, 4, 5]`. You can assume the input is always a list (possibly empty) and that the only non-list elements are integers. You may use recursion or iteration.

Constraints

- The input is a list with arbitrary nesting depth. - The only elements are either lists or integers. - The total number of integers in the input is at most 10,000. - Depth can be up to 1,000. - Do not use `itertools.chain` or any external libraries.

Example

>>> flatten_list([1, [2, 3], [[4]], 5])
[1, 2, 3, 4, 5]
>>> flatten_list([])
[]
>>> flatten_list([[1, [2]], 3])
[1, 2, 3]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think recursively: if the element is a list, flatten it and extend; otherwise, append it.
An empty list should flatten to an empty list.
Watch the order: preserve the order of integers as they appear.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.