Skip to content
Home / Fundamentals

How to Make a Flat List Out of a List of Lists in Python

In Python, a list of lists is a data structure that is used to store a list of elements, where each element is itself a list. For example, you might have a list of lists that represents a two-dimensional grid, or a list of lists that represents a set of customer data, where each inner list represents a customer's information.

Sometimes, you may need to flatten this list of lists into a single, flat list. In other words, you want to take all the elements in the inner lists and put them into a single, top-level list. This can be useful if you want to perform some operation on all the elements in the list of lists, but don't want to deal with the added complexity of nested lists.

In this article, we'll look at a few different ways to flatten a list of lists in Python.

Method 1: Using a For Loop

One of the most straightforward ways to flatten a list of lists is to use a for loop. This approach involves iterating over the inner lists and appending their elements to a new list one by one.

Here's an example of how you could do this:

# Given a list of lists
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Create an empty list to store the flat list
flat_list = []

# Iterate over the list of lists
for inner_list in list_of_lists:
  # Iterate over the inner list
  for item in inner_list:
    # Append the item to the flat list
    flat_list.append(item)

# Print the flat list
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

This code will create a new list called flat_list, which starts off as an empty list. It then iterates over the list_of_lists, and for each inner list, it iterates over the items in that list and appends them to the flat_list.

At the end, the flat_list will contain all the elements from the inner lists, in the order they appear.

Method 2: Using the itertools.chain Function

Another way to flatten a list of lists is to use the itertools.chain function from the itertools module. This function takes an iterable (such as a list) and returns an iterator that "chains" together the elements of the input iterable.

Here's an example of how you can use itertools.chain to flatten a list of lists:

import itertools

# Given a list of lists
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Use itertools.chain to flatten the list of lists
flat_list = list(itertools.chain(*list_of_lists))

# Print the flat list
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Method 3: Using List Comprehension

List comprehension is a concise way to create a list using a single line of code. You can use list comprehension to flatten a list of lists by concatenating the inner lists together using the + operator.

Here's an example of how you can use list comprehension to flatten a list of lists:

# Given a list of lists
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Use list comprehension to flatten the list of lists
flat_list = [item for inner_list in list_of_lists for item in inner_list]

# Print the flat list
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

This code creates a new list called flat_list, which is constructed using a list comprehension. The comprehension consists of two for loops, which iterate over the list_of_lists and the inner lists, respectively. For each item in the inner lists, it is appended to the flat_list.

At the end, the flat_list will contain all the elements from the inner lists, in the order they appear.

Method 4: Using the sum Function

Another way to flatten a list of lists is to use the sum function. The sum function takes an iterable and returns the sum of its elements. When used with a list of lists, the sum function will concatenate the inner lists together into a single list.

Here's an example of how you can use the sum function to flatten a list of lists:

# Given a list of lists
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Use the sum function to flatten the list of lists
flat_list = sum(list_of_lists, [])

# Print the flat list
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

This code calls the sum function with the list_of_lists as the first argument and an empty list [] as the second argument. The empty list serves as the starting value for the sum, and the sum function will add each element of the list_of_lists to it. Since the list_of_lists is a list of lists, the sum function will concatenate the inner lists together into a single list.

At the end, the flat_list will contain all the elements from the inner lists, in the order they appear.