I have nested dictionary of list.
inputlist= {1: {0: [[1, 20], [6, 20]], 1: [[3, 22], [4, 22]]},
2: {0: [[2, 21], [7, 21]], 1: [[3, 22], [4, 23]]},
3: {0: [[5, 23], [8, 22]], 1: [[7, 23], [3, 11]]}}
For each key 1,2 and 3, I want to do addition by column value for each key 0 and 1. Result should be like this:
{1: {0: [(7, 40)] 1: [(7, 44)]},
2: {0: [(9, 42)] 1: [(7, 45)]},
3: {0: [(13, 45)] 1: [(10, 34)]}}
Here what have I tried:
sum_result={}
for k1, v1 in inputlist.items():
for (k2,v2) in v1.items():
sum_result+= v2
print (sum_result)
try the following code to get your expected output:
inputlist= {1: {0: [[1, 20], [6, 20]], 1: [[3, 22], [4, 22]]},
2: {0: [[2, 21], [7, 21]], 1: [[3, 22], [4, 23]]},
3: {0: [[5, 23], [8, 22]], 1: [[7, 23], [3, 11]]}}
sum_result= dict()
for k1, v1 in inputlist.items():
sum = []
temp = {}
for (k2,v2) in v1.items():
sum = [v2[0][0]+v2[1][0], v2[0][1]+v2[1][1]]
temp[k2] = sum
sum_result[k1] = temp
print(sum_result)
Output:
{
1: {0: [7, 40], 1: [7, 44]},
2: {0: [9, 42], 1: [7, 45]},
3: {0: [13, 45], 1: [10, 34]}
}