AppDividend
Latest Code Tutorials

How to Flatten List in Python

0

The list is one of the most flexible data structures in Python. Transforming a list of lists into a list is called “flattening a list“. A Python list can contain multiple dimensions. This means that you have a list within lists. These lists sometimes referred to as “nested lists,” can be converted back into a regular list.

How to Flatten List

To flatten a list in Python, use either list comprehension or importing libraries like itertools or numpy will do the job. When it comes to nested lists, there are two types of nested lists.

  1. Irregular nested list
  2. Regular nested list

Every element of this list is a sublist, thereby adhering to the uniformity of the element type. Every element of this list is either a sublist or a non-list item. Therefore, there is an irregularity in terms of the element type.

Using List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of the existing list. A list comprehension consists of brackets containing the expression, which is executed for each element, and the for loop to iterate over each element.

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = [element for sublist in original_list for element in sublist]

print("Original list", original_list)

print("Flattened list", flatten_list)

Output

Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]

You can see that we have flattened a list. 

List comprehensions provide a concise way to create lists.

Flatten List of Lists Using itertools

Using itertools is ideal for transforming a 2D list into a single flat list. It treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument sequentially.

import itertools

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = list(itertools.chain(*original_list))

print("Original list", original_list)

print("Flattened list", flatten_list)

Output

Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]

And we got the flatten list in the output.

While itertools is an effective way at flattening a list, it is more advanced than the last approach we have discussed.

This is because you must import itertools into your code which introduces a new dependency. What’s more, the chain() method involves unpacking which can be difficult to understand.

Flatten List of Lists Using numpy (concatenate() and flat())

To flatten a list of lists in Python, use the combination of numpy library, concatenate(), and flat() function. Numpy offers common operations, which include concatenating regular 2D arrays row-wise or column-wise. We are also using the flat attribute to get a 1D iterator over the array to achieve our goal.

import numpy as np

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = list(np.concatenate(original_list). flat)

print("Original list", original_list)

print("Flattened list", flatten_list)

Output

Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]

That’s it for flattening a list in Python.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.