python

Create a flat list from list of lists in Python

A list is a collection of multiple items. A list can also contain multiple lists. If you have a list of lists and want to create a flat list from that list, you can the methods explained in this post.

# create a list of lists
my_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g'], ['h', 'i']]

# use list comprehension to generate falt list
result = [item for child_list in my_list for item in child_list]

# print the result
print(result)

Output

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Using list comprehension

As the above code example shows, we can use list comprehension to make a flat list from a list of lists. This can be considered the most popular and fastest way to do that. In the list comprehension, we pass our list in the loop and it will return a list that will contain all the items from a list of lists.

Check the following code example to understand it.

# create a list of lists
list_of_lists = [[10, 20, 30], [40, 50], [60, 70], [80]]

# use list comprehension to generate falt list
flat_list = [item for child_list in list_of_lists for item in child_list]

# print the result
print(flat_list)

Output

[10, 20, 30, 40, 50, 60, 70, 80]

User reduce() and iconcat to generate a flat list

We will use the reduce() function of the functools module and iconcat of the operator module to generate a flat list from a list of lists. We need to import functools and operator modules in our project file to use reduce() and iconcat.

Check the following example to understand it

import functools
import operator

# defined a nested list
mylist = [['a', 'b'], ['c', 'd', 'e'], ['f', 'g', 'h']]

# use reduce() and iconcat to create flat list
result = functools.reduce(operator.iconcat, mylist, [])

# print the result
print(result)

Output

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Generate flat list using itertools module

We can also use Python itertools module to generate a flat list using a list of lists. We need to import the itertools module in our Python file and use the itertools.chain() function to flatten the nested list.

import itertools

# defined a nested list
input_list = [['a', 'b'], ['c', 'd', 'e'], ['f', 'g', 'h']]

# generate the flat list from the above
result = list(itertools.chain(*input_list))

print(result)

Output

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

In the above code example:

1. We have imported the itertools module into our project file.

2. we have defined a list input_list that contains multiple lists in it.

3. Using itertools.chain() function and flattening the list of lists and storing them in result named variable.

4. Print the result.

# defined a nested list
mylist = [['a', 'b'], ['c', 'd', 'e'], ['f', 'g', 'h']]

result = []

# use for loop to create the flat list
for sublist in mylist:
  result.extend(sublist)

# print the result
print(result)

# -> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
If you do not want to use any module to convert a list of lists to a flat list then you can use python For loop to do that. In the above code example, we have defined a list that contains multiple lists We are using For loop to iterate through the list and extending the list item in each iteration.
Was this helpful?