---
title: "Flatten a nested list, Turn a nested list like [[1, 2], [3, 4]] into [1, 2, 3, 4]."  
description: "Flatten a nested list, Turn a nested list like [[1, 2], [3, 4]] into [1, 2, 3, 4]."  
author: "Anubhav Sharma"  
published: 2025-04-23  
updated: 2025-04-23  
canonical: https://www.mindstick.com/interview/34061/flatten-a-nested-list-turn-a-nested-list-like-1-2-3-4-into-1-2-3-4  
category: "python"  
tags: ["python-3.4", "python"]  
reading_time: 3 minutes  

---

# Flatten a nested list, Turn a nested list like [[1, 2], [3, 4]] into [1, 2, 3, 4].

### Problem Statement

Here are given a list that contains **other lists** inside it (one level deep). Our goal is to **combine all elements into a single flat list**.

### Approach

There are several ways to flatten a list in Python, especially for one-level deep nested lists. Here are two common approaches:

### Method 1: [Using List Comprehension](https://www.mindstick.com/interview/34016/what-are-list-comprehensions-in-python) (Recommended)

```python
nested_list = [[1, 2], [3, 4]]

# Flattening the list using list comprehension
flat_list = [item for sublist in nested_list for item in sublist]

print(flat_list)
```

#### Explanation:

1. `for sublist in nested_list`: Loops through each sublist, e.g., `[1, 2]` and `[3, 4]`.
2. `for item in sublist`: Loops through each item inside that sublist.
3. `item for sublist in nested_list for item in sublist`: This is a nested loop inside the list comprehension.

## Output:

```plaintext
[1, 2, 3, 4]
```

### Method 2: Using `itertools.chain`

```python
import itertools

nested_list = [[1, 2], [3, 4]]

flat_list = list(itertools.chain.from_iterable(nested_list))

print(flat_list)
```

#### Explanation:

1. `itertools.chain.from_iterable()` takes an iterable of iterables and flattens it.
2. You convert the result to a list using `list()`.

## Output:

```plaintext
[1, 2, 3, 4]
```

### Use Case

This is useful when you are dealing with 2D data structures such as a grid, spreadsheet or nested data and you want a simple, one-dimensional idea.

## Read More -

1. [What are list comprehensions in Python?](https://www.mindstick.com/interview/34016/what-are-list-comprehensions-in-python)
2. [What is the difference between break, continue, and pass?](https://www.mindstick.com/interview/34015/what-is-the-difference-between-break-continue-and-pass)
3. [What is the difference between mutable and immutable types in Python?](https://www.mindstick.com/interview/34017/what-is-the-difference-between-mutable-and-immutable-types-in-python)

## Answers

### Answer by Anubhav Sharma

### Problem Statement

Here are given a list that contains **other lists** inside it (one level deep). Our goal is to **combine all elements into a single flat list**.

### Approach

There are several ways to flatten a list in Python, especially for one-level deep nested lists. Here are two common approaches:

### Method 1: [Using List Comprehension](https://www.mindstick.com/interview/34016/what-are-list-comprehensions-in-python) (Recommended)

```python
nested_list = [[1, 2], [3, 4]]

# Flattening the list using list comprehension
flat_list = [item for sublist in nested_list for item in sublist]

print(flat_list)
```

#### Explanation:

1. `for sublist in nested_list`: Loops through each sublist, e.g., `[1, 2]` and `[3, 4]`.
2. `for item in sublist`: Loops through each item inside that sublist.
3. `item for sublist in nested_list for item in sublist`: This is a nested loop inside the list comprehension.

## Output:

```plaintext
[1, 2, 3, 4]
```

### Method 2: Using `itertools.chain`

```python
import itertools

nested_list = [[1, 2], [3, 4]]

flat_list = list(itertools.chain.from_iterable(nested_list))

print(flat_list)
```

#### Explanation:

1. `itertools.chain.from_iterable()` takes an iterable of iterables and flattens it.
2. You convert the result to a list using `list()`.

## Output:

```plaintext
[1, 2, 3, 4]
```

### Use Case

This is useful when you are dealing with 2D data structures such as a grid, spreadsheet or nested data and you want a simple, one-dimensional idea.

## Read More -

1. [What are list comprehensions in Python?](https://www.mindstick.com/interview/34016/what-are-list-comprehensions-in-python)
2. [What is the difference between break, continue, and pass?](https://www.mindstick.com/interview/34015/what-is-the-difference-between-break-continue-and-pass)
3. [What is the difference between mutable and immutable types in Python?](https://www.mindstick.com/interview/34017/what-is-the-difference-between-mutable-and-immutable-types-in-python)


---

Original Source: https://www.mindstick.com/interview/34061/flatten-a-nested-list-turn-a-nested-list-like-1-2-3-4-into-1-2-3-4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
