Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python merge two lists of dictionaries based on matching key

I have the following two data sets, and I’m looking to merge them in a very specific way:

Stream

stream = {
  "sdd": [{
    "eid": "e532fcdb-2f3f-4c51-9afb-996b8679a5da",
    "nid": "ebef3cb1-9053-4d1e-b409-b682236445b7",
    "nname": "client"
  }, {
    "eid": "901b51b1-851f-448e-b2cc-9730267f824b",
    "nid": "0b5d79b5-0d21-4186-b3db-8f4a9f2b25fb",
    "nname": "server"
  }]
}

Profile

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

profile = [{
  "ackid": "5dfe5dd4-e863-4474-bdad-54231b925f12",
  "nid": "ebef3cb1-9053-4d1e-b409-b682236445b7",
  "name": "default",
  "resources": {
    "cpu": {
      "min_cores": "2",
      "max_cores": "8"
    },
    "memory": {
      "min": "2000",
      "max": "10000"
    },
    "storage": {
      "min": "50",
      "max": "100"
    }
  }
}]

What I want to do is add to ‘stream’ from ‘profile’ the matching (min) values from ‘resources’ where both ‘nid’ match to get something like this:

Output

{
  "sdd": [{
    "eid": "e532fcdb-2f3f-4c51-9afb-996b8679a5da",
    "nid": "ebef3cb1-9053-4d1e-b409-b682236445b7",
    "nname": "client",
    "cpu": "2",
    "mem": "2000",
    "storage":"50"
  }, {
    "eid": "901b51b1-851f-448e-b2cc-9730267f824b",
    "nid": "0b5d79b5-0d21-4186-b3db-8f4a9f2b25fb",
    "nname": "server"
  }]
}

>Solution :

def get_dict(x): 
    return {'min':x['memory']['min'], 'cpu':x['cpu']['min_cores'], 
        'storage':x['storage']['min']} 



Out = {'ssd':[i|get_dict(j['resources']) if i['nid']==j['nid'] else i for i in 
    stream['sdd'] for j in profile]}

Out: 
{'ssd': [{'eid': 'e532fcdb-2f3f-4c51-9afb-996b8679a5da',
   'nid': 'ebef3cb1-9053-4d1e-b409-b682236445b7',
   'nname': 'client',
   'min': '2000',
   'cpu': '2',
   'storage': '50'},
  {'eid': '901b51b1-851f-448e-b2cc-9730267f824b',
   'nid': '0b5d79b5-0d21-4186-b3db-8f4a9f2b25fb',
   'nname': 'server'}]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading