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.
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:
This is concise and fast enough for most cases. Read it left to right as:
- loop over each
sublist - then loop over each
iteminside 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:
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:
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:
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_iterableis a clean standard-library alternative.' - '
extendin 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
- Flip two-dimensional associative array in PHP
- foreach vs someList.ForEach
- Format y axis as percent
- Four color theorem Java implementation of U.S. map
- Float types are not supported. Use Decimal types instead
- For i 0, why is i i equal to 0?
- Freezing graph to pb in Tensorflow2
- Frequency counts for unique values in a NumPy array

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 courseTrack 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.