I have several dictionaries (perhaps 10s of them) that formed like below:
{'stdout': [{'foo': 'A', 'bar': 'B', 'host': None, 'count': 135},
{'foo': 'C', 'bar': 'B', 'host': 'egg', 'count': 28},
{'foo': 'D', 'bar': 'E', 'host': 'apple', 'count': 1},
{'foo': 'A', 'bar': 'E', 'host': 'chicken breast', 'count': 1},
{'foo': 'C', 'bar': 'F', 'host': 'carrot', 'count': 1}],
'stderr': ''}
I want to combine all those dictionaries with adding 'count' key's integer with same 'foo','bar' and 'host' keys (None is NoneType)
For example, for 2 dictionaries
dictA = {'stdout': [{'foo': 'A', 'bar': 'B', 'host': None, 'count': 135},
{'foo': 'C', 'bar': 'B', 'host': 'egg', 'count': 28},
{'foo': 'D', 'bar': 'E', 'host': 'apple', 'count': 2},
{'foo': 'A', 'bar': 'E', 'host': 'chicken breast', 'count': 1},
{'foo': 'C', 'bar': 'F', 'host': 'carrot', 'count': 1}],
'stderr': ''}
dictB = {'stdout': [{'foo': 'A', 'bar': 'B', 'host': None, 'count': 280},
{'foo': 'A', 'bar': 'B', 'host': 'orange', 'count': 46},
{'foo': 'A', 'bar': 'E', 'host': 'pineapple', 'count': 3},
{'foo': 'D', 'bar': 'E', 'host': 'apple', 'count': 2},
{'foo': 'C', 'bar': 'F', 'host': 'carrot', 'count': 1}],
'stderr': ''}
Then the merged version should be
dictMerged = {'stdout': [{'foo': 'A', 'bar': 'B', 'host': None, 'count': 415},
{'foo': 'A', 'bar': 'B', 'host': 'orange', 'count': 46},
{'foo': 'C', 'bar': 'B', 'host': 'egg', 'count': 28},
{'foo': 'D', 'bar': 'E', 'host': 'apple', 'count': 4},
{'foo': 'A', 'bar': 'E', 'host': 'pineapple', 'count': 3},
{'foo': 'C', 'bar': 'F', 'host': 'carrot', 'count': 2},
{'foo': 'A', 'bar': 'E', 'host': 'chicken breast', 'count': 1}],
'stderr': ''}
Note that the dictionary elements in list's order changed after 'count' summed.
I have tried to combine them for same 'host' as a first step like below but it was not same as what I wanted:
hostname1 = {i["host"]: i for i in dictA['stdout']}
hostname2 = {i["host"]: i for i in dictB['stdout']}
all_host = hostname1|hostname2
{key: value + b[key] for key, value in a.items()}