Convert list of tuples to list?

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

How do I convert

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

to

[1, 2, 3]
11条回答
  •  忘掉有多难
    2020-12-02 13:36

    Using operator or sum

    >>> from functools import reduce ### If python 3
    >>> import operator
    >>> a = [(1,), (2,), (3,)]
    >>> list(reduce(operator.concat, a))
    [1, 2, 3]
    

    (OR)

    >>> list(sum(a,()))
    [1, 2, 3]
    >>> 
    

    If in python > 3 please do the import of reduce from functools like from functools import reduce

    https://docs.python.org/3/library/functools.html#functools.reduce

提交回复
热议问题