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.
- List Comprehension
- Using sum() Function
- With += operator
- chain() Function
- Functools – Reduce Operation
- Nested loop – iterative approach
- Numpy – Concatenate and flat
- Lambda
Table of Contents
List Comprehension
Flattening lists of lists into a regular list we can use list comprehension method. Like below:
Code Example
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)
Or
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)
Output

Using sum() Function
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.
Example Code
if __name__ == '__main__':
lists = [[1, 2, 3], [True, False], [6.1, 7.3, 8.0], ['a', 'b', 'c']]
joinedlist = sum(lists, [])
print(joinedlist)
Output

With += Operator
In this, we will use a for loop to add each element of the nested list into an empty list using += operator.
Example Code
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)
Output

itertools.chain() Function
We will use itertools.chain() Function to flatten lists of list into a single list by iterating list items.
Example Code
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)
Output

Functools – Reduce Operation
Here, it adds elements of original list using reduce the operation of functools module to flat list.
Example Code
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)
Output

Nested Loop
Using nested loops, we can append all items of nested list inside a new list. This way we can flatten lists of lists.
Example Code
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)
Output

Numpy Module – Concatenate and flat.
The Numpy library have many operations one of them is concatenation. This operation can be used to flatten lists.
Example Code
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)
Output

Lambda
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.
Example Code
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))
Output

Conclusion
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.