How to Flatten a list of lists in Python [6 Methods]

In this Python tutorial, I will explain how to flatten a list of lists in Python using different methods with some illustrative examples.

To flatten a list of lists in Python, various methods can be employed such as list comprehension for concise one-liners, traditional for loops for straightforward iteration, the sum() function for easy concatenation, recursion for handling nested lists, the reduce() function from functools for cumulative concatenation, and itertools.chain() for efficiently linking iterables.

Flatten a list of lists in Python Methods

There are six different methods to flatten a list of lists in Python:

  • Using list comprehension
  • Using nested for loop
  • Using Recursion
  • Using sum()
  • Using lambda and reduce()
  • Using itertools.chain()

Let’s see them one by one using some illustrative examples:

1. How to Flatten a List of Lists in Python using list comprehension

List comprehension is a concise way to create lists. This method uses a concise syntax to iterate over each sublist and then each element within the sublist, creating a flat list in a single line of code in Python.

This way we can use list comprehension to flatten a list of lists in Python:

landmarks = [["Statue of Liberty", "NY"], ["Golden Gate Bridge", "CA"], ["Grand Canyon", "AZ"]]
flattened_list = [item for sublist in landmarks for item in sublist]
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate Bridge', 'CA', 'Grand Canyon', 'AZ']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to Flatten a List of Lists in Python

2. How to make a flat list out of a list of lists using for loop

A for loop iterates over each sublist and then each item within those sublists, appending each element to a new flat list in Python. For instance:

landmarks = [["Statue of Liberty", "NY"], ["Golden Gate Bridge", "CA"], ["Grand Canyon", "AZ"]]
flattened_list = []
for sublist in landmarks:
    for item in sublist:
        flattened_list.append(item)
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate Bridge', 'CA', 'Grand Canyon', 'AZ']

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

flatten a list of nested lists in Python

3. Flatten List of Lists Recursively

Recursion is a method where the function calls itself. It’s useful for nested lists of unknown depth. For example:

def flatten_list(nested_list):
    if not nested_list:
        return nested_list
    if isinstance(nested_list[0], list):
        return flatten_list(nested_list[0]) + flatten_list(nested_list[1:])
    return nested_list[:1] + flatten_list(nested_list[1:])

landmarks = [["Statue of Liberty", ["NY"]], ["Golden Gate", ["CA"]], ["Grand Canyon", ["AZ"]]]
flattened_list = flatten_list(landmarks)
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate', 'CA', 'Grand Canyon', 'AZ']

After executing the code in Pycharm, one can see the output in the below screenshot.

flat list from nested lists in Python

4. Flatten list of lists using sum()

The sum() function in Python can concatenate all the sublists into a single flat list starting from an empty list. For instance:

landmarks = [["Statue of Liberty", "NY"], ["Golden Gate Bridge", "CA"], ["Grand Canyon", "AZ"]]
flattened_list = sum(landmarks, [])
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate Bridge', 'CA', 'Grand Canyon', 'AZ']

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

Flatten an irregular list of lists in Python

5. Python flatten a list of lists using lambda with reduce() function

The reduce() function from the functools module and a lambda function to cumulatively concatenate sublists into a single list. For example:

from functools import reduce

landmarks = [["Statue of Liberty", "NY"], ["Golden Gate Bridge", "CA"], ["Grand Canyon", "AZ"]]
flattened_list = reduce(lambda x, y: x + y, landmarks)
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate Bridge', 'CA', 'Grand Canyon', 'AZ']

After executing the code in Pycharm, one can see the output in the below screenshot.

Flatten list containing lists of lists in Python

6. Python Program to Flatten a Nested List using itertools.chain()

The chain() function from the itertools module, is specifically designed to iterate through a series of iterables, effectively chaining them into a single iterable in Python.

This is how we can use the chain() function to flatten a list of lists in Python:

from itertools import chain

landmarks = [["Statue of Liberty", "NY"], ["Golden Gate Bridge", "CA"], ["Grand Canyon", "AZ"]]
flattened_list = list(chain(*landmarks))
print(flattened_list)

Output:

['Statue of Liberty', 'NY', 'Golden Gate Bridge', 'CA', 'Grand Canyon', 'AZ']

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

Flatten List of Lists in Python

Conclusion

There are many ways to flatten a list of lists in Python, each with advantages. We can use quick and easy methods like list comprehension or for loops, or more advanced techniques like recursion, sum(), lambda with reduce(), and itertools.chain(). Each method works well in different situations, allowing us to choose the best one for our needs.

You may also like to read: