How do make a flat list out of list of lists in Python?


A nested list is a list which has elements as lists itself.

For example: [[1,2,3], [4,5,6], [7,8,9]] is a nested list as it has 3 lists ([1,2,3], [4, 5, 6], and [7, 8, 9]) as its elements.

To flatten a list of lists (nested list) i.e., to convert a 2D list into a 1D list, there are different ways such as nested for loop, list comprehension, built-in functions or by importing libraries in python.

In this article we will discuss the above methods to flatten a list in Python.

Using for loops

For iterating repeatedly through a sequence, use a for loop (that is either a list, a tuple, a dictionary, a set, or a string). This functions more like an iterator method seen in other object-oriented programming languages and is less like the for keyword found in other programming languages.

Example

In the following example code, we must transform a nested list into a flat list which is a single 1D list. So, we have used the append() method to add elements into the list.

def flatlist(l): flat_list = [] for element in l: if type(element) is list: for item in element: flat_list.append(item) else: lat_list.append(element) return flat_list lst = [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]] print('Original List', lst) print('Flattend List', flatlist(lst))

Output

The following output is obtained on executing the above program.

Original List [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]]
Flattend List [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Using List Comprehension

Python's list comprehension feature makes it simple to build new lists from pre-existing ones. It offers a more concise syntax for building new lists from of pre-existing ones and their values.

Example

In this example, the nested list is converted into a single 1D list using the list comprehension method.

original_list = [[10, 20, 30, 40], [50, 60, 70], [80, 90]] flat_list = [item for sublist in original_list for item in sublist] print('Original list', original_list) print('Flattend list', flat_list)

Output

The following output is obtained on executing the above program.

 Original list [[10, 20, 30, 40], [50, 60, 70], [80, 90]]
Flattend list [10, 20, 30, 40, 50, 60, 70, 80, 90]

Using NumPy library

A general-purpose package for handling arrays is called NumPy. It offers a multidimensional array object with outstanding speed as well as capabilities for interacting with these arrays.

Example

In the following example we used the NumPy library with other methods in it. Those method are mentioned below.

  • concatenate()function is used to concatenate a sequence of arrays.

  • flat() function is used as a 1D iterator over an N Dimensional array.

import numpy originallist = [[10, 20, 30, 40], [50, 60, 70], [80, 90]] flatlist = list(numpy.concatenate(originallist).flat) print('Original list', originallist) print('Transformed list', flatlist)

Output

The following output is obtained on executing the above program.

Original list [[10, 20, 30, 40], [50, 60, 70], [80, 90]]
Transformed list [10, 20, 30, 40, 50, 60, 70, 80, 90]

Updated on: 08-Nov-2022

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements