← Back to challenges

Flatten a List

PythonHardrecursionarrays

Instructions

Create a function that takes a list. This list can have all kinds of primitives, even other lists. The function should return a single, flat, one-dimensional, list with all elements. Here are the conditions:

  1. If the item is a List, include each item in it and the following still apply:
  2. If the item is a Primitive, include it as is.

Examples

flatten_list([1, 2, [3, [4, 5], 6], 7, 8]) ➞ [1, 2, 3, 4, 5, 6, 7, 8]

flatten_list([1]) ➞ [1]

flatten_list([]) ➞ []

Notes

If no input is given it should return an empty list: [].

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.