One-Level Flatten ๐ŸŒฑ
README.md
main.py
notes.txt
Output
Tests

One-Level Flatten

๐ŸŒฑ Beginner Project 3529667130

๐Ÿฆ„ One-Level Flatten

Difficulty: Beginner Tags: lists, arrays, iteration Series: CS 101


Problem

In the mystical realm, magical lists can contain nested treasures. Your task is to flatten a list by exactly one level - no more, no less.

Given a list that may contain nested sublists, flatten it by one level. If an element is a list, extract its contents into the result. If an element is not a list, add it directly. Do not recursively flatten deeper than one level.

Real-World Application

One-level flattening appears in: - Data processing - normalizing API responses with nested arrays - Argument handling - processing variadic function arguments - Database queries - flattening joined result sets - Configuration files - merging nested config sections - Event handling - combining event handler arrays - Functional programming - partial application and currying

Input

data = list[Any]  # List that may contain nested lists

Output

list[Any]  # Flattened list (one level deep only)

Constraints

  • Total elements (including nested) โ‰ค 100,000
  • Only flatten one level deep
  • Preserve order of elements

Examples

Example 1: Basic Flattening

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

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

Explanation: - 1 is not a list โ†’ add 1 - [2, 3] is a list โ†’ add 2, then 3 - 4 is not a list โ†’ add 4 - [5] is a list โ†’ add 5 - Result: [1, 2, 3, 4, 5]

Example 2: Empty List

Input: []

Output: []

Explanation: - No elements to flatten

Example 3: No Nested Lists

Input: [1, 2, 3]

Output: [1, 2, 3]

Explanation: - No sublists to flatten - Return as-is

Example 4: All Nested

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

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

Explanation: - Each element is a list, extract all contents

Example 5: Deeply Nested (Only Flatten One Level)

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

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

Explanation: - 1 โ†’ add 1 - [2, [3, 4]] is a list โ†’ add 2, add [3, 4] (keep inner list intact!) - 5 โ†’ add 5 - Only flatten one level, not recursively

Example 6: Mixed Types

Input: ['a', ['b', 'c'], 1, [2, 3]]

Output: ['a', 'b', 'c', 1, 2, 3]

Explanation: - Works with any data types, not just integers


What You'll Learn

  • How to check if an element is a list/iterable
  • Iterating and extending results conditionally
  • The difference between shallow and deep flattening
  • Using extend() vs append() in Python

Why This Matters

One-level flattening is a common operation in data processing. Interviews test whether you understand the distinction between shallow and deep operations, and can implement precise transformations.



Starter Code

def challenge_function(data):
    """
    Flatten a list by exactly one level.

    Args:
        data: list that may contain nested lists

    Returns:
        list: flattened list (one level only)
    """
    # Your implementation here
    pass
OUTPUT
TESTS
Output
Code execution results displayed hereโ€ฆ
Test Results
Test results displayed hereโ€ฆ