python - get list of tuples first index?

后端 未结 5 1142
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 02:48

What\'s the most compact way to return the following:

Given a list of tuples, return a list consisting of the tuples first (or second, doesn\'t matter) elements.

5条回答
  •  -上瘾入骨i
    2020-12-03 03:12

    use zip if you need both

    >>> r=(1,'one'),(2,'two'),(3,'three')
    >>> zip(*r)
    [(1, 2, 3), ('one', 'two', 'three')]
    

提交回复
热议问题