PIXELBANKv9.1.0
Menu

Flatten Nested List Iterator

Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer or a list of integers (which may also be nested).

Input: nested list as string (e.g., [[1,1],2,[1,1]]). Output: all integers space-separated.

Example:

Input:
[[1,1],2,[1,1]]
Output:
1 1 2 1 1
Reasoning:
  • The input [[1,1],2,[1,1]] is a nested list containing two sublists and one integer.
  • We iterate through the list, flattening each sublist: the first sublist [1,1] becomes 1 1, the integer 2 remains 2, and the last sublist [1,1] becomes 1 1.
  • We concatenate the flattened elements, resulting in the sequence 1 1 2 1 1.
  • The final output is the concatenated sequence of integers, separated by spaces: 1 1 2 1 1.

Constraints:

  • 1 <= total integers <= 500
  • Nesting depth <= 50
solution.py

Test Results

0/0
Run code to see test results.
Flatten Nested List Iterator - Medium | PixelBank