Convert list of tuples to list?

后端 未结 11 1143
旧巷少年郎
旧巷少年郎 2020-12-02 12:54

How do I convert

[(1,), (2,), (3,)]

to

[1, 2, 3]
11条回答
  •  情话喂你
    2020-12-02 13:44

    @Levon's solution works perfectly for your case.

    As a side note, if you have variable number of elements in the tuples, you can also use chain from itertools.

    >>> a = [(1, ), (2, 3), (4, 5, 6)]
    >>> from itertools import chain
    >>> list(chain(a))
    [(1,), (2, 3), (4, 5, 6)]
    >>> list(chain(*a))
    [1, 2, 3, 4, 5, 6]
    >>> list(chain.from_iterable(a)) # More efficient version than unpacking
    [1, 2, 3, 4, 5, 6]
    

提交回复
热议问题