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.
- Reading a nested comprehension
- The order of the for clauses
- itertools.chain as an alternative
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.
nested📦 Three boxes of two marbles eachfor group in nested📤 Open one box after anotherfor value in group🔵 Take out each marbleflattened🥣 One bowl with all six marblesFlattening turns a list of lists into a single list containing all the inner items in order.
Key points
for group in nestedis the outer loop: it visits each inner list.for value in groupis the inner loop: it visits each value in that list.- The loops appear in the same order as two nested
forloops would. flattenedholds[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.chainavoids building a new list.
Example code
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)
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Writing the loops in the wrong order
The inner loop cannot come first, because group does not exist yet.
[value for value in group for group in nested]
[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.
- Flatten a 2D Array Combine the rows of a 2D integer array into one array. Easy
- Flatten Arbitrarily Nested Lists (Recursion) Practice recursion by flattening arbitrarily nested lists while skipping null entries. Medium