flatten list of list through list comprehension

前端 未结 5 745
闹比i
闹比i 2020-12-01 21:15

I am trying to flatten a list using list comprehension in python. My list is somewhat like

[[1, 2, 3], [4, 5, 6], 7, 8]

just for printing

5条回答
  •  半阙折子戏
    2020-12-01 21:44

    No-one has given the usual answer:

    def flat(l):
      return [y for x in l for y in x]
    

    There are dupes of this question floating around StackOverflow.

提交回复
热议问题