Python
list
flatten
programming
tutorial

Flattening a shallow list in Python

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A shallow nested list is a list whose elements are themselves lists, but only one level deep. Flattening it means turning something like [[1, 2], [3, 4], [5]] into [1, 2, 3, 4, 5], and in Python there are a few clean ways to do that depending on your priorities.

Use a List Comprehension for the Most Pythonic Solution

For everyday code, a nested list comprehension is usually the clearest approach:

python
nested = [[1, 2], [3, 4], [5]]
flat = [item for sublist in nested for item in sublist]
print(flat)

This is concise and fast enough for most cases. Read it left to right as:

  • loop over each sublist
  • then loop over each item inside that sublist
  • collect all items into one new list

Because the structure is only one level deep, this solution stays readable.

Use itertools.chain for an Explicit Iterator-Based Approach

If you want a standard-library tool that expresses concatenation directly, use itertools.chain.from_iterable:

python
1from itertools import chain
2
3nested = [[1, 2], [3, 4], [5]]
4flat = list(chain.from_iterable(nested))
5print(flat)

This is a strong choice when you want to emphasize that the sublists are being chained together. It is also convenient when the input is any iterable of iterables, not necessarily a literal list of lists.

Use extend in a Loop When You Need More Control

An explicit loop is slightly longer, but it is easy to customize:

python
1nested = [[1, 2], [3, 4], [5]]
2flat = []
3
4for sublist in nested:
5    flat.extend(sublist)
6
7print(flat)

This approach is useful when you need to validate or transform each sublist before extending the result. It is also easy for beginners to read because nothing is compressed into one expression.

Know What "Shallow" Means

These techniques assume the nesting depth is one level. They do not recursively flatten arbitrary structures.

For example:

python
nested = [[1, 2], [3, [4, 5]]]

A shallow flattening approach will not recursively open [4, 5]. That is a different problem and requires a recursive function or stack-based traversal.

That distinction matters because many bugs come from applying a shallow solution to a deeply nested structure by mistake.

Common Pitfalls

The biggest mistake is using string or bytes inputs as if they were normal sublists. Flattening [["ab"], ["cd"]] is different from flattening ["ab", "cd"], because strings are themselves iterable and can be split into characters unexpectedly in some generic flattening code.

Another issue is choosing a recursive deep-flatten solution when the data is only one level deep. That adds complexity for no benefit.

Developers also sometimes use repeated list concatenation inside a loop, such as flat = flat + sublist. That creates unnecessary intermediate lists and is slower than extend.

Finally, if memory matters, remember that all of these examples create a new list. That is usually fine, but it is still a copy rather than a view.

Summary

  • A shallow flatten turns a one-level nested list into a flat list.
  • A nested list comprehension is the most common Python solution.
  • 'itertools.chain.from_iterable is a clean standard-library alternative.'
  • 'extend in a loop is useful when you need extra processing or clarity.'
  • Do not confuse shallow flattening with recursive deep flattening.
  • For very large inputs, generator-based chaining can delay list creation until the final conversion step.
  • If the input may contain tuples as well as lists, the same shallow techniques still apply.
  • Explicit assumptions about depth prevent subtle data-shape bugs.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.