1>>> from collections import Iterable
2def flatten(lis):
3 for item in lis:
4 if isinstance(item, Iterable) and not isinstance(item, str):
5 for x in flatten(item):
6 yield x
7 else:
8 yield item
9
10>>> lis = [1,[2,2,2],4]
11>>> list(flatten(lis))
12[1, 2, 2, 2, 4]
13>>> list(flatten([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
14[1, 2, 3, 4, 5, 6, 7, 8, 9]