How does the list comprehension to flatten a python list work?

后端 未结 5 1449
忘了有多久
忘了有多久 2020-12-30 06:50

I recently looked for a way to flatten a nested python list, like this: [[1,2,3],[4,5,6]], into this: [1,2,3,4,5,6].

Stackoverflow was helpful as ever and I found a

5条回答
  •  渐次进展
    2020-12-30 07:36

    For the lazy dev that wants a quick answer:

    >>> a = [[1,2], [3,4]]
    >>> [i for g in a for i in g]
    [1, 2, 3, 4]
    

提交回复
热议问题