Flattening a List of Lists in Python (How-To)



When working with nested lists in Python, you may come across a scenario where you need to convert the nested list into a single list containing all the elements. This process is commonly referred to as “flattening” the list. Flattening a list of lists can be a useful task in various scenarios, such as data processing or when working with complex JSON data structures.

In this article, we will explore different approaches to flatten a list of lists in Python.

Method 1: Using List Comprehension

One of the simplest and most efficient ways to flatten a list of lists is by using list comprehension. List comprehension allows us to create a new list by iterating over the elements of the nested list and adding them to the new list.

def flatten_list(nested_list):
    return [element for sublist in nested_list for element in sublist]

The above code defines a function flatten_list that takes a nested list as input and returns a flattened list using list comprehension. By iterating over each sublist in the nested list and then iterating over each element in the sublist, we can add all the elements to the new flattened list.

Here’s an example usage of the flatten_list function:

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = flatten_list(my_list)
print(flattened_list)

The output of the above code will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Method 2: Using Recursion

Another approach to flatten a list of lists is by using recursion. Recursion is a programming technique where a function calls itself to solve a smaller version of the problem.

def flatten_list(nested_list):
    flattened_list = []
    for element in nested_list:
        if isinstance(element, list):
            flattened_list.extend(flatten_list(element))
        else:
            flattened_list.append(element)
    return flattened_list

The above code defines a function flatten_list that takes a nested list as input and returns a flattened list using recursion. The function iterates over each element in the nested list. If the element is itself a list, the function calls itself with the element as input and extends the flattened list. If the element is not a list, it is directly appended to the flattened list.

Here’s an example usage of the flatten_list function:

my_list = [[1, 2, 3], [4, [5, 6]], [7, 8, 9]]
flattened_list = flatten_list(my_list)
print(flattened_list)

The output of the above code will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Method 3: Using itertools.chain

The itertools.chain function from the itertools module can also be used to flatten a list of lists in Python.

import itertools

def flatten_list(nested_list):
    return list(itertools.chain(*nested_list))

The above code imports the itertools module and defines a function flatten_list that takes a nested list as input and returns a flattened list using itertools.chain. The itertools.chain function takes multiple iterables as arguments and returns a single iterator that produces the elements of each iterable in sequence.

Here’s an example usage of the flatten_list function:

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = flatten_list(my_list)
print(flattened_list)

The output of the above code will be:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Conclusion

Flattening a list of lists in Python is a common task when working with nested data structures. In this article, we explored three different methods to flatten a list of lists: using list comprehension, recursion, and the itertools.chain function. Each method has its own advantages and can be used depending on the specific requirements of your project.

By understanding these techniques, you can efficiently flatten nested lists and simplify your data processing tasks in Python.