Reference an Element in a List of Tuples

后端 未结 7 420
无人共我
无人共我 2020-12-08 10:37

Sorry in advance, but I\'m new to Python. I have a list of tuples, and I was wondering how I can reference, say, the first element of each tuple w

7条回答
  •  旧巷少年郎
    2020-12-08 11:04

    You can get a list of the first element in each tuple using a list comprehension:

    >>> my_tuples = [(1, 2, 3), ('a', 'b', 'c', 'd', 'e'), (True, False), 'qwerty']
    >>> first_elts = [x[0] for x in my_tuples]
    >>> first_elts
    [1, 'a', True, 'q']
    

提交回复
热议问题