Skip to content Skip to sidebar Skip to footer

Combine Or Merge Dictionaries Based On More Than One Key Or Multi Keys

I've been searching all morning but most examples of merging was based on only one key, I couldn't find anything on multiple keys. x = [ {'pid':111, 'sid':6, 'eid':123, 'x_qty'

Solution 1:

This is re-keying off a tuple:

>>> from operator import itemgetter
>>> from collections import defaultdict
>>> data = defaultdict(dict)
>>> f = itemgetter('pid', 'sid', 'eid')
>>> for d in [*x, *y]:
...     data[f(d)].update(d)
... >>> list(data.values())
[{'eid': 123, 'pid': 111, 'sid': 6, 'x_qty': 30, 'y_qty': 123},
 {'eid': 6212, 'pid': 222, 'sid': 56, 'x_qty': 2},
 {'eid': 6212, 'pid': 333, 'sid': 56, 'y_qty': 112}]

Solution 2:

Here is an alternative using 3rd party library pandas, which accepts a list of dictionaries.

import pandas as pd

# merge data
merged = pd.DataFrame(x).merge(pd.DataFrame(y), how='outer')

# iterate, remove nan, convert to int
res = [s.dropna().astype(int).to_dict() for _, s in merged.iterrows()]

[{'eid': 123, 'pid': 111, 'sid': 6, 'x_qty': 30, 'y_qty': 123},
 {'eid': 6212, 'pid': 222, 'sid': 56, 'x_qty': 2},
 {'eid': 6212, 'pid': 333, 'sid': 56, 'y_qty': 112}]

Post a Comment for "Combine Or Merge Dictionaries Based On More Than One Key Or Multi Keys"