lapinkoira
lapinkoira

Reputation: 8988

How to sum two lists of tuples

list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]


list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

How could you combine them into a single list of tuples? considering the first value of the key appears in both lists.

Upvotes: 0

Views: 694

Answers (4)

Agost Biro
Agost Biro

Reputation: 2839

You can use the following snippet if the lists are not guaranteed to have the same length or the keys are not guaranteed to be present in both lists or they are not in the same order:

from collections import defaultdict

list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]
list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

d = defaultdict(int, list1)
for key, n in list2:                   
    d[key] += n
res = list(map(tuple, d.items()))

res:

[(1533945600000, 1656),
 (1534032000000, 2588),
 (1534118400000, 2410),
 (1534204800000, 2888),
 (1534291200000, 8642),
 (1534377600000, 2462),
 (1534464000000, 1742),
 (1534550400000, 943),
 (1534636800000, 1615),
 (1534723200000, 1419),
 (1534809600000, 1243),
 (1534896000000, 970)]

Upvotes: 2

Kaushik NP
Kaushik NP

Reputation: 6781

Using List comprehension with zip :

>>> [(x[0], x[1]+y[1]) for x,y in zip(list1, list2)]

#driver values :

IN : list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]
     list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

OUT : [(1533945600000, 1656), (1534032000000, 2588), (1534118400000, 2410), (1534204800000, 2888), (1534291200000, 8642), (1534377600000, 2462), (1534464000000, 1742), (1534550400000, 943), (1534636800000, 1615), (1534723200000, 1419), (1534809600000, 1243), (1534896000000, 970)]

Upvotes: 2

Ori Arbes
Ori Arbes

Reputation: 313

combined_list=[]
for i,j in zip(list1,list2):
     combined_list.append((i[0]+j[0],i[1]+j[1]))

Upvotes: 1

Space Impact
Space Impact

Reputation: 13255

use zip on list1,list2 and then add second value of each tuple:

lst = []
for tup1,tup2 in zip(list1,list2):
    sum_ = tup1[1]+tup2[1]
    lst.append((tup1[0],sum_))

lst

[(1533945600000, 1656),
 (1534032000000, 2588),
 (1534118400000, 2410),
 (1534204800000, 2888),
 (1534291200000, 8642),
 (1534377600000, 2462),
 (1534464000000, 1742),
 (1534550400000, 943),
 (1534636800000, 1615),
 (1534723200000, 1419),
 (1534809600000, 1243),
 (1534896000000, 970)]

Upvotes: 3

Related Questions