# Explode List

> One row holds many values. Unpack it.

Canonical URL: <https://datadriven.io/problems/explode_list>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list containing inner lists, flatten ONE level only: yield each inner list's elements in order. Do not recurse into deeper nesting.

## Worked solution and explanation

### Why this problem exists in real interviews

One-level list flattening tests whether you understand the difference between **shallow** and **deep** flattening. It checks that you extend sublists without recursing into deeper nesting.

---

### Break down the requirements

#### Step 1: Check if each element is a list

For elements that are lists, extend the result with their contents. For non-list elements, append directly.

#### Step 2: Do not recurse

Only flatten one level. If a sublist contains another list, that inner list remains as-is.

---

### The solution

**Single-level flatten with isinstance check**

```python
def flatten(nested):
    result = []
    for item in nested:
        if isinstance(item, list):
            for sub in item:
                result.append(sub)
        else:
            result.append(item)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of elements after flattening. Each element is visited once.
> 
> **Space:** O(n) for the output list.

> **Interviewers Watch For**
>
> Explicitly using `isinstance(item, list)` and not recursing. This shows you read the requirement carefully and do not over-engineer.

> **Common Pitfall**
>
> Recursing into deeper levels when the problem only asks for one level of flattening. Read the depth requirement carefully.

---

## Common follow-up questions

- What if the input contains tuples that should also be flattened? _(Tests broadening the isinstance check to include tuple.)_
- How would you generalize this to flatten exactly N levels? _(Tests a loop-based approach that applies one-level flattening N times.)_
- What if elements include strings, which are iterable? _(Tests excluding strings from the flattening check since iterating a string yields characters.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/explode_list)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.