简体   繁体   中英

How to merge dictionaries within two lists if they have a common key-value pair?

I have two lists of dictionaries. Each dictionary in each list has an id field. I'd like to merge the dictionaries in List 1 with the dictionaries in List 2, only if they share a common 'id'

L1 = [{'id':'123','field1':'abc','field2':'def','field3':'ghi'},{'id':'456','field1':'jkl','field2':'mno','field3':'pqr'},{'id':'789','field1':'stu','field2':'vwx','field3':'yz'}] 
L2 = [{'field4':'aaa','field5':'bbb','field6':'ccc','field7':'ddd','id':'101'},{'field4':'eee','field5':'fff','field6':'ggg','field7':'hhh','id':'789'},{'field4':'iii','field5':'jjj','field6':'kkk','field7':'lll','id':'456'},{'field4':'mmm','field5':'nnn','field6':'ooo','field7':'ppp','id':'123'}]


DesiredList = [{'id':'123','field1':'abc','field2':'def','field3':'ghi','field4':'mmm','field5':'nnn','field6':'ooo','field7':'ppp'},{'id':'456','field1':'jkl','field2':'mno','field3':'pqr','field4':'iii','field5':'jjj','field6':'kkk','field7':'lll'},{'id':'789','field1':'stu','field2':'vwx','field3':'yz','field4':'eee','field5':'fff','field6':'ggg','field7':'hhh'},{'field4':'aaa','field5':'bbb','field6':'ccc','field7':'ddd','id':'101'}]

I've done

DesiredList = []
for Dict2 in L2:
    for Dict1 in L1:
        if Dict1['id'] == Dict2['id']:
            Dict2.update(Dict1)
    DesiredList.append(Dict2)

This results in

  >>>DesiredList 
  DesiredList = [{'id': '101', 'field6': 'ccc', 'field7': 'ddd', 'field4': 'aaa', 'field5': 'bbb'}, {'field2': 'vwx', 'field3': 'yz', 'field1': 'stu', 'field6': 'ggg', 'field7': 'hhh', 'field4': 'eee', 'field5': 'fff', 'id': '789'}, {'field2': 'mno', 'field3': 'pqr', 'field1': 'jkl', 'field6': 'kkk', 'field7': 'lll', 'field4': 'iii', 'field5': 'jjj', 'id': '456'}, {'field2': 'def', 'field3': 'ghi', 'field1': 'abc', 'field6': 'ooo', 'field7': 'ppp', 'field4': 'mmm', 'field5': 'nnn', 'id': '123'}]

There must be a better way to do this that is quicker and more efficient.

If you are looking for an efficient way, you could do like this

from itertools import chain
from collections import defaultdict

result = defaultdict(dict)

for c_dict in chain(L1, L2):
    result[c_dict["id"]].update(c_dict)

print result.values()

Output

[{'field1': 'abc',
  'field2': 'def',
  'field3': 'ghi',
  'field4': 'mmm',
  'field5': 'nnn',
  'field6': 'ooo',
  'field7': 'ppp',
  'id': '123'},
 {'field1': 'stu',
  'field2': 'vwx',
  'field3': 'yz',
  'field4': 'eee',
  'field5': 'fff',
  'field6': 'ggg',
  'field7': 'hhh',
  'id': '789'},
 {'field1': 'jkl',
  'field2': 'mno',
  'field3': 'pqr',
  'field4': 'iii',
  'field5': 'jjj',
  'field6': 'kkk',
  'field7': 'lll',
  'id': '456'},
 {'field4': 'aaa',
  'field5': 'bbb',
  'field6': 'ccc',
  'field7': 'ddd',
  'id': '101'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM