flatten list of list through list comprehension

前端 未结 5 739
闹比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:48

    You're trying to iterate through a number, which you can't do (hence the error).

    If you're using python 2.7:

    >>> from compiler.ast import flatten
    >>> flatten(l)
    [1, 2, 3, 4, 5, 6, 7, 8]
    

    But do note that the module is now deprecated, and no longer exists in Python 3

提交回复
热议问题