Reputation: 303
I am trying to append key value pair to an existing list of dict by comparing the key column.
# Initial list of dict
lst = [{'NAME': 'TEST', 'TYPE': 'TEMP'},
{'NAME': 'TEST', 'TYPE': 'PERMANENT'},
{'NAME': 'TEST1', 'TYPE': 'TEMP'}]
# From the below list of dict, the Key (NAME only) need to compare with the initial list dict
# and for any match need to append the rest of key value pair to the initial list of dict.
compare_lst = [{'NAME': 'TEST', 'ID': 70001, 'V_KEY': 67, 'R_KEY': 1042, 'W_ID': 22},
{'NAME': 'TEST1','ID': 70005, 'V_KEY': 75, 'R_KEY': 1047, 'W_ID': 28}]
# Expected Out put
out = [{'NAME': 'TEST', 'TYPE': 'TEMP', 'ID': 70001, 'V_KEY': 67, 'R_KEY': 1042, 'W_ID': 22},
{'NAME': 'TEST', 'TYPE': 'PERMANENT', 'ID': 70001, 'V_KEY': 67, 'R_KEY': 1042, 'W_ID': 22},
{'NAME': 'TEST1', 'TYPE': 'TEMP', 'ID': 70005, 'V_KEY': 75, 'R_KEY': 1047, 'W_ID': 28}]
Upvotes: 0
Views: 2307
Reputation: 140178
simple approach, no list comps, double loop but works fine:
out=[]
for l in lst:
for c in compare_lst:
if l['NAME']==c['NAME']: # same "key": merge
lc = dict(l)
lc.update(c) # lc is the union of both dictionaries
out.append(lc)
print(out)
more pythonic, after having defined an helper function to return the union of 2 dictionaries:
out=[]
def dict_union(a,b):
r = dict(a)
r.update(b)
return r
for l in lst:
out.extend(dict_union(l,c) for c in compare_lst if l['NAME']==c['NAME'])
n python 3.5+, no need for union aux method, you could write more simply
out = []
for l in lst:
out.extend({**l,**c} for c in compare_lst if l['NAME']==c['NAME'])
even better: oneliner using itertools.chain
to flatten the result of the 2-level list comp
import itertools
out=list(itertools.chain(dict_union(l,c) for l in lst for c in compare_lst if l['NAME']==c['NAME'] ))
for python 3.5
import itertools
out=list(itertools.chain({**l,**c} for l in lst for c in compare_lst if l['NAME']==c['NAME'] ))
(the multiple methods of doing it materialize the incremental improvements I made those last minutes)
Upvotes: 1