Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 12

Flatten a single-level list of lists

Medium

16 minute session

Summary

Flatten only one level of nesting: turn a list of lists (and/or tuples and scalars) into a single list of elements.

Problem statement

Write a function flatten_once(nested) that accepts a list which may contain other lists or tuples as elements and returns a new list with exactly one level of flattening applied. For each element in the input list: - If the element is a list or tuple, extend the output with its elements (one level deep). - Otherwise, append the element as-is. Do not perform deep flattening: if an inner element contains nested lists beyond the first level, leave those nested structures intact as elements.

Task

Implement a function that flattens one level of nesting for a list containing sublists/tuples and other elements.

Examples

Basic flatten

Input

[[1, 2], [3, 4]]

Output

[1, 2, 3, 4]

Explanation

Each inner list is expanded one level into the resulting list.

Input format

A single Python list which may contain lists, tuples, or other objects as elements.

Output format

A list with one level of flattening applied.

Constraints

Only flatten one level. Preserve the order of elements. Result must be a list.

Samples

Sample input 0

[['a'], ['b', 'c'], []]

Sample output 0

['a', 'b', 'c']

Explanation 0

Empty inner lists contribute nothing; other inner lists are expanded.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

09:11 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.