Convert list of tuples to list?

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

How do I convert

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

to

[1, 2, 3]
11条回答
  •  甜味超标
    2020-12-02 13:44

    In these situations I like to do:

    a = [(1,), (2,), (3,)]
    new_a = [element for tup in a for element in tup]
    

    This works even if your tuples have more than one element. This is equivalent to doing this:

    a = [(1,), (2,), (3,)]
    new_a = []
    for tup in a:
        for element in tup:
            new_a.append(element)
    

提交回复
热议问题