The Depth of Field
A easy Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
Problem
A config export arrives as arbitrarily nested lists, but the consumer downstream only handles a fixed number of nesting levels. Collapse `lst` by exactly `depth` levels, preserving order, so a `depth` of 0 returns it untouched and anything still nested below the levels you collapse stays wrapped in its remaining sublists. A `depth` larger than the actual nesting simply flattens as far as the data goes.
Summary
Some containers hold containers that hold containers.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.