I have 4 lists of dictionaries and each list contains 4 dictionaries. The lists look similar to this:
A = [
{'1': 150, '2': 160, '3': 140, '4': 110},
{'1': 230, '2': 650, '3': 134, '4': 650},
{'1': 220, '2': 530, '3': 980, '4': 3450},
{'1': 150, '2': 160, '3': 440, '4': 110}]
B = [
{'1': 165, '2': 430, '3': 134, '4': 650},
{'1': 64, '2': 650, '3': 345, '4': 340},
{'1': 220, '2': 650, '3': 340, '4': 134},
{'1': 150, '2': 160, '3': 234, '4': 2340}]
C = [
{'1': 678, '2': 430, '3': 134, '4': 650},
{'1': 344, '2': 650, '3': 345, '4': 340},
{'1': 220, '2': 650, '3': 340, '4': 134},
{'1': 150, '2': 160, '3': 234, '4': 2340}]
D = [
{'1': 165, '2': 430, '3': 134, '4': 650},
{'1': 64, '2': 650, '3': 345, '4': 340},
{'1': 220, '2': 650, '3': 340, '4': 134},
{'1': 150, '2': 160, '3': 234, '4': 1440}]
I would like to sum the values of the first dictionary of each list, where the keys are the same (so for key '1', then for '2', then for '3' and so on ...), together with the values of the first dictionary of the rest of the lists.
Then do the same for the second, third and fourth dictionary.
The expected outcome should be 4 dictionaries, that maintain the order of the keys while summing up the values for the keys that are the same.
I have tried the piece of code below:
for i in range (4):
dict1 = A[i]
dict2 = B[i]
dict3 = C[i]
dict4 = D[i]
# adding the values with common key
Cdict = Counter(dict1) + Counter(dict2) + Counter(dict3) + Counter(dict4)
print(Cdict)
But the problem is that the order of the keys is changing:
Counter({'4': 2060, '2': 1450, '1': 1158, '3': 542})
Counter({'2': 2600, '4': 1670, '3': 1169, '1': 702})
Counter({'4': 3852, '2': 2480,'3': 2000, '1': 880})
Counter({'4': 6230, '3': 1142, '2': 640, '1': 600})