I want update first dictonery based on second dictonery key value

时间:2019-05-27 09:00:49

标签: python dictionary

I have a nested list of dictionary which i want to update based on another list of dictionary. if key of first dictionary match with another then update new key value for that. and both dictionary are not in same order.

I am trying to update using for loop but that takes a lot time to update. because i have a huge data to update.


ldict1 = [{"key": "2", "order": "1", 'child': [{"key": "4", "order": "1.1", 'child': []}]},
          {"key": "1", "order": "2", 'child': [{"key": "3", "order": "2.1", 'child': []}]},
          ]
ldict2 = [{"key": "1", 'name': 'name1', 'child': [{"key": "3", 'name': 'name4', 'child': []}]},
          {"key": "2", 'name': 'name2', 'child': [{"key": "4", 'name': 'name3', 'child': []},
                                                  {"key": "5", 'name': 'name5', 'child': []}]},
          ]


for d in ldict1:
   for j in ldict2:
       if i['key'] == j['key']:
          i['order'] = j['order']
          .....
          .....



Output:

output = [
    {"key": "1", 'name': 'name1', "order": "2", 'child': [{"key": "3", 'name': 'name3', "order": "2.1", 'child': []}]
     },
    {"key": "2", 'name': 'name2', "order": "1", 'child': [{"key": "4", 'name': 'name4', "order": "1.1",'child': []},
                                                          {"key": "5", 'name': 'name5', "order": "1.2",'child': []}]},
    ]

0 个答案:

没有答案