Jason TK
Jason TK

Reputation: 265

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':30},
    {'pid':222, 'sid':56, 'eid':6212, 'x_qty':2}
    ]

y = [
    {'pid':111, 'sid':6, 'eid':123, 'y_qty':123},
    {'pid':333, 'sid':56, 'eid':6212, 'y_qty':112}
    ]

Values of pid=111, sid=6, eid=123 match in both x and y, then combine as one record. If they don't match, just bring it over as is.

FINAL RESULTS THAT I WANT:

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

Upvotes: 4

Views: 626

Answers (2)

jpp
jpp

Reputation: 164623

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}]

Upvotes: 0

wim
wim

Reputation: 362557

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}]

Upvotes: 4

Related Questions