-1

Is there any way to do following task in python?, i have a 2 or more tuples of string in python in that sometimes 1st string is different and all others are the same so I want output like the following-

a1=[('1','a','b','3'),('2','a','b','3')]
# yields ['1','2']

a2=[('23','j','k','l'),('2','j','k','l'), ('34','j','k','l')]
# yields ['23','2','34']
Wenfang Du
  • 8,804
  • 9
  • 59
  • 90

1 Answers1

1

You can use zip for this. Default result is tuple, so we convert back to list via map.

a1 = [('1','a','b','3'), ('2','a','b','3')]

res = next(map(list, zip(*a1)))

# ['1', '2']

res2 = next(map(list, zip(*a2)))

# ['23', '2', '34']
jpp
  • 159,742
  • 34
  • 281
  • 339