1flat_list = [item for sublist in l for item in sublist]
2
3#which is equivalent to this
4flat_list = []
5for sublist in l:
6 for item in sublist:
7 flat_list.append(item)
1# idiomatic python
2
3# using itertools
4import itertools
5
6list_of_list = [[1, 2, 3], [4, 5], [6]]
7chain = itertools.chain(*images)
8
9flattened_list = list(chain)
10# [1, 2, 3, 4, 5, 6]
1l = [[1, 2], [3, 4], [5, 6, 7]]
2flat_list = [item for sublist in l for item in sublist]
3# flat_list = [1, 2, 3, 4, 5, 6, 7]
1def flatten(L):
2 for item in L:
3 try:
4 yield from flatten(item)
5 except TypeError:
6 yield item
7
8list(flatten([[[1, 2, 3], [4, 5]], 6]))
9>>>[1, 2, 3, 4, 5, 6]
10