Python
lists
join
programming
code-duplicate

join list of lists in python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When people ask how to join a list of lists in Python, they usually mean flattening one nesting level into a single list. Python has a few good ways to do that, and the best choice depends on whether you want a real list immediately, an iterator, or support for arbitrarily deep nesting. The important part is to be clear about the shape of the input before picking the tool.

Flatten One Level With a List Comprehension

For a simple list of lists, the most common solution is a nested list comprehension.

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

This is fast, idiomatic, and easy to read once you know the pattern. Read it left to right as:

  • for each group in nested
  • for each item in that group
  • emit the item

This preserves the original order of all elements.

Use itertools.chain for Iterator Pipelines

If you want to stream the values instead of creating a list immediately, itertools.chain.from_iterable is a good fit.

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

This is useful when the flattened values feed another iterator-based operation. It can also reduce peak memory use because values are produced lazily.

If you do want a list in the end, wrap it with list(...).

Avoid sum(nested, [])

You may see this pattern online:

python
nested = [[1, 2], [3, 4], [5, 6]]
flat = sum(nested, [])
print(flat)

It works for tiny examples, but it repeatedly concatenates lists and performs unnecessary copying. That makes it a poor choice for larger inputs.

So while it looks compact, it is usually not the answer you want in real code.

Deep Flattening Is a Different Problem

If the nesting depth is not fixed at one level, you need a different function.

python
1from collections.abc import Iterable
2
3
4def flatten_deep(values):
5    for value in values:
6        if isinstance(value, Iterable) and not isinstance(value, (str, bytes)):
7            yield from flatten_deep(value)
8        else:
9            yield value
10
11
12data = [1, [2, [3, 4]], [5, 6]]
13print(list(flatten_deep(data)))

This solves a broader problem, but it also changes semantics. Strings and bytes need special handling, or else they will be split into characters.

That is why one-level flattening and deep flattening should be treated as separate tasks.

Flattening and Joining Into a String Are Not the Same Step

Sometimes the real goal is not a flat Python list, but a final string. In that case, flatten first, then join.

python
1nested_words = [["join", "list"], ["of", "lists"]]
2flat_words = [word for group in nested_words for word in group]
3line = " ".join(flat_words)
4print(line)

Keeping those steps separate makes the code easier to debug. Flattening changes structure. Joining formats text.

Validate the Input Shape When It Matters

In application code, a flattening helper is often fed data from external APIs or ETL steps. If the shape is not guaranteed, consider validating that each top-level element is actually iterable before flattening. It is better to fail clearly than to silently flatten the wrong structure and discover the bug much later in the pipeline.

Common Pitfalls

  • Using sum(nested, []) and paying for repeated list concatenation.
  • Applying a one-level solution to data that can be nested more deeply.
  • Forgetting to exclude strings and bytes in deep-flatten helpers.
  • Trying to flatten and join text in one unreadable expression.

Summary

  • Use a nested list comprehension for clear one-level flattening.
  • Use itertools.chain.from_iterable when lazy iteration is useful.
  • Avoid sum for flattening because it scales poorly.
  • Deep flattening is a different problem and needs different rules.
  • Separate flattening from text-joining so each step stays obvious.

Course illustration
Course illustration

All Rights Reserved.