Home > database >  How to zip a list with a list of tuples?
How to zip a list with a list of tuples?

Time:02-04

Say I have the following lists:

list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]

How do I combine them so that it becomes

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

CodePudding user response:

You can expand the tuple in the second zipped item to build a final tuple

list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]

print([(a,*b) for a,b in zip(list_a, list_b)])

Output

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

CodePudding user response:

list_a = [1, 4, 7]
list_b = [(2, 3), (5, 6), (8, 9)]
# FOR LIST_A TO B
    
for i in range(len(list_a)):
    a = list_a[i]
    test = list_b[i]   (a,)
    print(test)
  • Related