1# example of a nested list
2my_list = [[1, 2], ["one", "two"]]
3
4# accessing a nested list
5my_list[1][0] # outputs "one"
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]
1L = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
2for list in L:
3 for number in list:
4 print(number, end=' ')
5# Prints 1 2 3 4 5 6 7 8 9