Problem 434078 · medium · Phase 04 Non-Linear Data Structures

Unpack the Shipping Crates

recursion · nested lists · flattening

A warehouse scanner receives crate contents as an arbitrarily nested list: each element is either an item (any non-list value) or a sub-crate (another list). Write unpack(items) returning a single flat list of all items in the order they are encountered from left to right, diving into every sub-crate.

Examples

Input:  items = [1, [2, [3, 4]], 5]
Output: [1, 2, 3, 4, 5]

Input:  items = [[], [[]], []]
Output: []

Constraints

  • At most 10**4 items in total; items may be integers or strings.
  • Nesting depth at most 1500 (recursion stays under the limit).

Goals

  • Build a flat list from an arbitrarily nested one
  • Extend the result with the flattened sub-list rather than appending it
  • Keep left-to-right order across every depth
Starting Python…