Pedro Gonçalves Pedro Gonçalves - 1 month ago 6
Python Question

How to form a new tuple with the first value of each tuple of a tuple of tuples?

I want to form a tuple with the first elements of each tuple. Im a beginner in python, maybe this is an easy one but i cant find a way to do it plz help.

ex:

input

x = (('A','B','C'),('D','E','F'),('G','H','I'))


output:

y= ('A', 'D', 'G')

CAB CAB
Answer

Use comprehension and convert to a tuple;

>>> x = (('A','B','C'),('D','E','F'),('G','H','I'))
>>> y = tuple([l[0] for l in x])
>>> y
('A', 'D', 'G')
Comments