Last Updated On By Anmol Lohana
Flatten a list of lists in Python is converting a nested list into a normal single list. It means the process of converting the 2D list into the 1D list. For example, converting [[1, 2, 3], [‘a’, ‘b’, ‘c’], [1.1, 3.0, 7.4], [True, False]] into [1, 2, 3, ’a’, ’b’, ’c’, 1.1, 3.0, 7.4, True, False].
Performing the example below:
The list is one of the most flexible data structures in Python. One can create single list objects or multiple list objects, or nested list-objects means 2-D list means lists inside of list in their programs. Creating a nested list is easy, but when a programmer wants all lists inside a list together in the form of a single list, they need to flatten it.
There are many ways to perform this flatten operation. We will discuss some of them in this tutorial.
Table of Contents
Flattening lists of lists into a regular list we can use list comprehension method. Like below:
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = []
[joinedlist.extend(list) for list in lists]
print(joinedlist)
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinlist = [x for list in lists for x in list]
print(joinlist)
The sum() function will add the list elements inside of lists and return a single list. To convert lists of lists into a single list, we will add the nested list into an empty list to get a regular list.
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = sum(lists, [])
print(joinedlist)
In this, we will use a for loop to add each element of the nested list into an empty list using += operator.
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = []
for list in lists:
joinedlist += list
print(joinedlist)
We will use itertools.chain() Function to flatten lists of list into a single list by iterating list items.
import itertools
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = list(itertools.chain(*lists))
print(joinedlist)
Here, it adds elements of original list using reduce the operation of functools module to flat list.
import operator
from functools import reduce
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = reduce(operator.add, lists)
print(joinedlist)
Using nested loops, we can append all items of nested list inside a new list. This way we can flatten lists of lists.
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = []
for i in range(len(lists)):
for j in range (len(lists[i])):
joinedlist.append(lists[i][j])
print(joinedlist)
The Numpy library have many operations one of them is concatenation. This operation can be used to flatten lists.
import numpy
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = list(numpy.concatenate(lists).flat)
print(joinedlist)
Lambda keyword is used to define an anonymous function. A list will pass as an argument in the function, and the lambda function will convert them from 2D to 1D or original list to flattened list.
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = lambda lists:[element for item in lists for element in joinedlist(item)] if type(lists) is list else [lists]
print(joinedlist(lists))
In conclusion, we discussed how to flatten a list of lists in Python. We saw the process using eight different ways with their coding examples or Python program to understand it more clear.