Pedro Gonçalves Pedro Gonçalves - 1 year ago 61
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.



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


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

Answer Source

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')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download