Flatten a list in python

前端 未结 7 1424
逝去的感伤
逝去的感伤 2020-12-06 19:14

I have a list like this:

[[(video1,4)], [(video2,5),(video3,8)], [(video1,5)], [(video5, 7), (video6,9)]...]

each item in this list may con

7条回答
  •  悲&欢浪女
    2020-12-06 20:04

    If this list is singly-nested (list of lists) you can do this, which I use a lot:

    flat_list = sum(list_of_lists, [])
    

    This works due to the fact that sum simply adds up the lists, and adding up lists works in python as expected :)

    Note: This is inefficient and some say unreadable.

提交回复
热议问题