Creating a Python list from a list of tuples

前端 未结 6 1885
一个人的身影
一个人的身影 2021-01-16 17:57

If I have, for example, a list of tuples such as

a = [(1,2)] * 4

how would I create a list of the first element of each tuple? That is,

6条回答
  •  旧时难觅i
    2021-01-16 18:25

    Two alternatives to phihag's list comprehension:

    [x for x, y in a]
    
    from operator import itemgetter
    map(itemgetter(0), a)
    

提交回复
热议问题