Python comment aplatir une liste
flat_list = [item for sublist in l for item in sublist]
Talented Thrush
flat_list = [item for sublist in l for item in sublist]
l = [[1, 2], [3, 4], [5, 6, 7]]
flat_list = [item for sublist in l for item in sublist]
# flat_list = [1, 2, 3, 4, 5, 6, 7]
flat_list = [item for sublist in l for item in sublist]
#which is equivalent to this
flat_list = []
for sublist in l:
for item in sublist:
flat_list.append(item)
flattened = [val for sublist in list_of_lists for val in sublist]
# idiomatic python
# using itertools
import itertools
list_of_list = [[1, 2, 3], [4, 5], [6]]
chain = itertools.chain(*images)
flattened_list = list(chain)
# [1, 2, 3, 4, 5, 6]
# flatten a list
flst = [[1,2,3], [8,9,12,17],[3,7]]
flatten_list = sorted(sum(flst,[]))
print(flatten_list) # [1, 2, 3, 3, 7, 8, 9, 12, 17]