Recursively flatten nested lists
Problem
Implement flatten(value). If value is a list, recursively flatten each element; otherwise return a one-element list containing value.
Starter code
def flatten(value):
passReveal answer or reference solution
def flatten(value):
if not isinstance(value, list):
return [value]
out = []
for element in value:
out.extend(flatten(element))
return outPublic tests
flatten([1,[2,[],[3]],4])→[1, 2, 3, 4]flatten(7)→[7]flatten([])→[]
Local history
Loading attempts saved in this browser…
Use with your agent
Share this URL and your attempt. Ask the agent to start with a clarifying question or the smallest useful hint.
Tutor me on https://mlprep.iwase.dev/programming/recursion/original-py-flatten/. If window.mlPrepAgent is available, read attempts for item original-py-flatten before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record and read it back.