flatten list of lists of lists to a list of lists

前端 未结 4 1418
耶瑟儿~
耶瑟儿~ 2020-12-07 03:27

I\'ve already searched SO for how to flatten a list of lists (i.e. here:Making a flat list out of list of lists in Python) but none of the solutions I find addresses flatten

4条回答
  •  抹茶落季
    2020-12-07 04:00

    If we apply the logic from this answer, should not it be just:

    In [2]: [[item for subsublist in sublist for item in subsublist] for sublist in my_list]
    Out[2]: [[1, 2, 3, 4, 5], [9, 8, 9, 10, 3, 4, 6], [1]]
    

    And, similarly via itertools.chain():

    In [3]: [list(itertools.chain(*sublist)) for sublist in my_list]
    Out[3]: [[1, 2, 3, 4, 5], [9, 8, 9, 10, 3, 4, 6], [1]]
    

提交回复
热议问题