Join a list of lists in Python

avatar

Borislav Hadzhiev

Last updated: Jun 20, 2022

banner

Photo from Unsplash

Join a list of lists in Python #

To join a list of lists in Python:

  1. Declare a new variable and set it to an empty list.
  2. Use a for loop to iterate over the two-dimensional list.
  3. Use a nested for loop to iterate over each nested list.
  4. Append each item to the empty list.
main.py
my_2d_list = [['a', 'b'], ['c', 'd'], ['e', 'f']] flat_list = [] for l in my_2d_list: for item in l: flat_list.append(item) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(flat_list)

We used a nested for loop to join a list of lists.

The outer for loop is used to iterate over the two-dimensional list.

In order to get access to the items in the nested lists, we need a nested for loop.

On each iteration, we append each list item into an empty list.

You could use this approach with one more nested loop if you had a 3D list.

You can use a list comprehension to achieve the same result.

main.py
my_2d_list = [['a', 'b'], ['c', 'd'], ['e', 'f']] flat_list = [item for l in my_2d_list for item in l] # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(flat_list)

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The list comprehension in the example is very similar to the for loop we used, but the syntax is a bit harder to read.

We still have 2 for loops but instead of appending the items of the nested list to an empty list, we simply return them from the list comprehension.

Alternatively, you can use the itertools.chain.from_iterable method.

Use the itertools.chain.from_iterable() method to join a list of lists, e.g. list(itertools.chain.from_iterable(my_2d_list)). The method uses a nested for loop under the hood and joins the list of lists into a single flat list.

main.py
import itertools my_2d_list = [['a', 'b'], ['c', 'd'], ['e', 'f']] flat_list = list(itertools.chain.from_iterable(my_2d_list)) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(flat_list)

The itertools.chain.from_iterable method can be used to achieve the same result.

The method is roughly equivalent to the following function.

main.py
def from_iterable(iterables): for it in iterables: for element in it: yield element

Another alternative to consider is to use the built-in sum() function.

To join a list of lists:

  1. Pass the two-dimensional list and an empty list to the sum() function.
  2. The sum function will sum the elements of the list and use the empty list as the start value.
  3. The result is a flat list containing the items.
main.py
my_2d_list = [['a', 'b'], ['c', 'd'], ['e', 'f']] flat_list = sum(my_2d_list, []) # 👇️ ['a', 'b', 'c', 'd', 'e', 'f'] print(flat_list)

The sum function takes an iterable and a start argument, sums them from left to right and returns the total.

When we sum two lists, we get a single list containing the items from the two lists.

main.py
# 👇️ ['a', 'b', 'c', 'd'] print(['a', 'b'] + ['c', 'd']) # 👇️ [1, 2, 3, 4] print([1, 2] + [3, 4])

By default the start argument is set to 0, so we have to explicitly pass an empty list.

Note that this approach is a bit more inefficient than using a nested for loop or the itertools.chain.from_iterable method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.