Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 18

Flatten One-Level List

Medium

17 minute session

Summary

Flatten only one level of nested lists, leaving deeper nested lists intact.

Problem statement

Write a function that takes a list which may contain elements that are themselves lists. The function should return a new list where any element that is a list is replaced by its elements (one-level flattening). Do not recursively flatten deeper nested lists — they should remain as elements. Preserve the order of elements. This practice focuses on loop control, type checking, and constructing new lists.

Task

Create a function that flattens elements that are lists by one level, practicing iteration and type checks.

Examples

Mixed elements

Input

[1, [2, 3], 4]

Output

[1, 2, 3, 4]

Explanation

The inner list [2, 3] is flattened into the outer list. Other elements stay the same.

Input format

A single list passed to flatten_one_level(lst). Elements may be any type; lists should be flattened one level.

Output format

A new list with one-level flattening applied.

Constraints

Input list length up to 10,000; total number of elements after flattening will also fit in memory. Only flatten Python list instances (type list). Do not flatten tuples or other iterables.

Samples

Sample input 0

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

Sample output 0

[1, 2, 3, 4, 5]

Explanation 0

All inner lists are expanded into the returned list.

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.

05:04 AM

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