TransWikia.com

How to merge dictionaries with the same key and value in Python

Stack Overflow Asked by Arbin Bulaybulay on January 26, 2021

I have a list that should be merge with the same key ‘client’ and merge those with the same value in key ‘statuses’ with the same date key.

items = [{'client': 'AAA', 'jobname': '1_Daily', 'statuses': {'09092020': 'Success', '09082020': 'Success', '09072020': 'Success', '09062020': 'Success', '09052020': 'Success', '09042020': 'Success', '09032020': 'Success'}}
{'client': 'AAA', 'jobname': '2_Daily', 'statuses': {'09092020': 'Success', '09082020': 'Failed', '09072020': 'Success', '09062020': 'Success', '09052020': 'Success', '09042020': 'Success', '09032020': 'Success'}}
{'client': 'BBB', 'jobname': 'Exchange_1', 'statuses': {'09092020': 'Success', '09082020': 'Success', '09072020': 'Success', '09062020': 'Success', '09052020': 'Success', '09042020': 'Success', '09032020': 'Success'}}
{'client': 'BBB', 'jobname': 'Exchange_2', 'statuses': {'09092020': 'Success', '09082020': 'Success', '09072020': 'Success', '09062020': 'Success', '09052020': 'Success', '09042020': 'Success', '09032020': 'Success'}}
{'client': 'CCC', 'jobname': 'Daily_1', 'statuses': {'09092020': 'Success', '09082020': 'Success', '09072020': 'Success', '09062020': 'Success', '09052020': 'Success', '09042020': 'Success', '09032020': 'Success'}}
{'client': 'CCC', 'jobname': 'Daily_2', 'statuses': {'09092020': 'Success', '09082020': 'Running', '09072020': 'Success', '09062020': 'Running', '09052020': 'Success', '09042020': 'Success', '09032020': 'Running'}}]

I need to arrive to something like this

jobs = {
'AAA': {'09092020': ['Success', 'Success'], '09082020': ['Success', 'Failed'], '09072020': ['Success', 'Success'], '09072020': ['Success', 'Success'], '09062020': ['Success', 'Success'], '09052020': ['Success', 'Success'], '09042020': ['Success', 'Success'], '09032020': ['Success', 'Success']},
'BBB': {'09092020': ['Success', 'Success'], '09082020': ['Success', 'Success'], '09072020': ['Success', 'Success'], '09072020': ['Success', 'Success'], '09062020': ['Success', 'Success'], '09052020': ['Success', 'Success'], '09042020': ['Success', 'Success'], '09032020': ['Success', 'Success']},
'CCC': {'09092020': ['Success', 'Success'], '09082020': ['Success', 'Running'], '09072020': ['Success', 'Success'], '09072020': ['Success', 'Success'], '09062020': ['Success', 'Running'], '09052020': ['Success', 'Success'], '09042020': ['Success', 'Success'], '09032020': ['Success', 'Running']}
}

Here is my code

from collections import defaultdict

jobs = {}
dd = defaultdict(list)
for item in items:
    for k, v in item.items():
        if k == "statuses":
            for key, val in v.items():
                dd[key].append(val)
            jobs[item['client']] = dd

One Answer

It would be much easier here to use a nested defaultdict to group multiple levels of keys:

from collections import defaultdict
from pprint import pprint

jobs = defaultdict(lambda: defaultdict(list))

for item in items:
    for code, status in item["statuses"].items():
        jobs[item["client"]][code].append(status)

pprint(jobs)

Output:

defaultdict(<function <lambda> at 0x000002349BD051F0>,
            {'AAA': defaultdict(<class 'list'>,
                                {'09032020': ['Success', 'Success'],
                                 '09042020': ['Success', 'Success'],
                                 '09052020': ['Success', 'Success'],
                                 '09062020': ['Success', 'Success'],
                                 '09072020': ['Success', 'Success'],
                                 '09082020': ['Success', 'Failed'],
                                 '09092020': ['Success', 'Success']}),
             'BBB': defaultdict(<class 'list'>,
                                {'09032020': ['Success', 'Success'],
                                 '09042020': ['Success', 'Success'],
                                 '09052020': ['Success', 'Success'],
                                 '09062020': ['Success', 'Success'],
                                 '09072020': ['Success', 'Success'],
                                 '09082020': ['Success', 'Success'],
                                 '09092020': ['Success', 'Success']}),
             'CCC': defaultdict(<class 'list'>,
                                {'09032020': ['Success', 'Running'],
                                 '09042020': ['Success', 'Success'],
                                 '09052020': ['Success', 'Success'],
                                 '09062020': ['Success', 'Running'],
                                 '09072020': ['Success', 'Success'],
                                 '09082020': ['Success', 'Running'],
                                 '09092020': ['Success', 'Success']})})

Answered by RoadRunner on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP