Beginner Python Exercises

Flatten a Nested List in Python

Turn [[1, 2], [3, 4], [5, 6]] into [1, 2, 3, 4, 5, 6] with a nested list comprehension.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Reading a nested comprehension
  • The order of the for clauses
  • itertools.chain as an alternative
Groups, then one flat list
[1, 2]
[3, 4]
[5, 6]
1
2
3
4
5
6

Loop over groups, then over items

The problem

Data often arrives in groups: rows of a table, pages of results, scores per class. Sometimes you need all values in one flat list to sum, sort or search them, without caring which group they came from.

Think of it like this

Flattening a nested list is like emptying several boxes of marbles into one big bowl. You open the first box and pour out every marble, then the next box, and so on. The bowl ends up with all marbles in the same order.

From boxes of marbles to code
nested📦 Three boxes of two marbles each
for group in nested📤 Open one box after another
for value in group🔵 Take out each marble
flattened🥣 One bowl with all six marbles
The outer loop opens the boxes, the inner loop empties each one.
In one sentence

Flattening turns a list of lists into a single list containing all the inner items in order.

Key points

  1. for group in nested is the outer loop: it visits each inner list.
  2. for value in group is the inner loop: it visits each value in that list.
  3. The loops appear in the same order as two nested for loops would.
  4. flattened holds [1, 2, 3, 4, 5, 6].

When should you use it?

  • Combining rows or groups into one list.
  • Preparing nested data for sorting, summing or searching.
  • Practicing comprehensions with two loops.

When not to use it

  • Lists nested more than one level deep: this only flattens one level, so use a recursive function.
  • Huge data you only loop over once: itertools.chain avoids building a new list.

Example code

Run code →
main.py
nested = [[1, 2], [3, 4], [5, 6]]
# Read every value from every inner list.
flattened = [value for group in nested for value in group]
print(flattened)
Get a Run button for this example

Result of running the code

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

Code explanation

The comprehension contains two loops written in the same order as nested for loops: first for group in nested, then for value in group. For every inner list, every value is added to the new list. The result keeps the original order of the values, [1, 2, 3, 4, 5, 6].

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Writing the loops in the wrong order

The inner loop cannot come first, because group does not exist yet.

Problem
[value for value in group for group in nested]
Fix
[value for group in nested for value in group]

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges