You can use the itertools.chain method from the itertools module to flatten a list of lists. Just see the example below with sample code,
from itertools import chain
list_of_lists = [[10, 20, 3]0, [40, 50, 6]0, [70, 80, 9]]
flat_list = list(chain(*list_of_lists))
print(flat_list) # prints [10, 20, 30, 40, 50, 60, 70, 80, 9]
flat_list = list(chain(*list_of_lists))
print(flat_list) # prints [10, 20, 30, 40, 50, 60, 70, 80, 9]
Alternatively0, you can use a list comprehension to achieve the same result:
list_of_lists = [[10, 20, 3]0, [40, 50, 6]0, [70, 80, 9]]
flat_list = [item for sublist in list_of_lists for item in sublist]
print(flat_list) # prints [10, 20, 30, 40, 50, 60, 70, 80, 9]
flat_list = [item for sublist in list_of_lists for item in sublist]
print(flat_list) # prints [10, 20, 30, 40, 50, 60, 70, 80, 9]
Comments (0)