CodeTestValidate

No installations needed! Write Python directly in your browser and get instant feedback with our smart validation system.

Challenge # 11

Flatten a Nested List

Objective

Flatten a list that contains elements and one-level nested lists into a single list.

Instructions

  • You are given a list called data.

  • The list may contain individual elements and sublists (1 level deep).

  • Flatten the list so that all values appear in a single list.

  • Print the flattened list.

Constraints:
Do not use input(). • The list may only be nested 1 level deep.
Exemple IO

Input: data = [1, [2, 3], 4, [5, 6]]

Output: [1, 2, 3, 4, 5, 6]

Input: data = [[1, 2], [3, 4], 5]

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

Input: data = [1, 2, 3]

Output: [1, 2, 3]

Try It Online!

Output:

Validate Your Results

Get your code reviewed and validated to understand what you did right and where you can improve.

Previous challengeCapitalize First Letter of Each WordNext challengeBasic Calculator
Loading...