Reputation: 13
I have two lists of tuples:
result = [(10002,), (10003,), (10004,), (10005,)...]
result1 = [('PL42222941176247135539240187',), ('PL81786645034401957047621964',), ('PL61827884040081351674977449',)...]
I want merge lists.
DESIRED OUPUT:
joined_result = [('PL42222941176247135539240187', 10002,), ('PL81786645034401957047621964', 10003,),('PL61827884040081351674977449', 10004,)...]
I think about zip, but is litte wrong.
[(('PL42222941176247135539240187',), (10002,)), (('PL81786645034401957047621964',), (10003,)), (('PL61827884040081351674977449',), (10004,))...]
How get desired output?
Upvotes: 0
Views: 135
Reputation: 118
Use:
result = [(10002,), (10003,), (10004,)]
result1 = [('PL42222941176247135539240187',), ('PL81786645034401957047621964',), ('PL61827884040081351674977449',)]
newResult = []
for i in range(len(result)):
result1[i] += result[i]
newResult.append(result1[i])
Warning!: it will break result1, so do result2 = result1 somewhere before the loop
Upvotes: 0
Reputation: 147
It seems zip fails because you have a list of tuples not only a list. In fact it creates a list of tuples of tuples. Convert your list of tuples before passing in zip:
result = [x[0] for x in result]
result1 = [x[0] for x in result1]
joined_result = list(zip(result1, result))
or in one line:
joined_result = list(zip([x[0] for x in result1],[x[0] for x in result]))
Upvotes: 0
Reputation: 51152
Just destructure the single-element tuples when you iterate using zip
:
joined_result = [(x, y) for ((x,), (y,)) in zip(result1, result)]
Upvotes: 2