Example 1: Using List Comprehension
my_list = [[1], [2, 3], [4, 5, 6, 7]]
flat_list = [num for sublist in my_list for num in sublist]
print(flat_list)
Output
[1, 2, 3, 4, 5, 6, 7]
This is the one of the simplest pythonic ways of flattening a list.
Example 2: Using Nested for Loops (non pythonic way)
my_list = [[1], [2, 3], [4, 5, 6, 7]]
flat_list = []
for sublist in my_list:
for num in sublist:
flat_list.append(num)
print(flat_list)
Output
[1, 2, 3, 4, 5, 6, 7]
An empty list flat_list
is created where each element is appended from the original list.
Example 3: Using itertools package
import itertools
my_list = [[1], [2, 3], [4, 5, 6, 7]]
flat_list = list(itertools.chain(*my_list))
print(flat_list)
Output
[1, 2, 3, 4, 5, 6, 7]
Example 4: Using sum()
my_list = [[1], [2, 3], [4, 5, 6, 7]]
flat_list = sum(my_list, [])
print(flat_list)
Output
[1, 2, 3, 4, 5, 6, 7]
Example 5: Using lambda and reduce()
from functools import reduce
my_list = [[1], [2, 3], [4, 5, 6, 7]]
print(reduce(lambda x, y: x+y, my_list))
Output
[1, 2, 3, 4, 5, 6, 7]