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

How to merge list of dictionaries by unique key value

I want to merge list of dictionary provided below with unique channel and zrepcode.

sample input:

[
  {
    "channel": 1,
    "zrepcode": "123456",
    "turn": 7833.9
  },
  {
    "channel": 1,
    "zrepcode": "123456",
    "pipeline": 324
  },
  {
    "channel": 1,
    "zrepcode": "123456",
    "inv_bal": 941.16
  },
  {
    "channel": 1,
    "zrepcode": "123456",
    "display": 341
  },
  {
    "channel": 3,
    "zrepcode": "123456",
    "display": 941.16
  },
  {
    "channel": 3,
    "zrepcode": "123456",
    "turn": 7935.01
  },
  {
    "channel": 3,
    "zrepcode": "123456",
    "pipeline": 0
  },
  {
    "channel": 3,
    "zrepcode": "123456",
    "inv_bal": 341
  },
  {
    "channel": 3,
    "zrepcode": "789789",
    "display": 941.16
  },
  {
    "channel": 3,
    "zrepcode": "789789",
    "turn": 7935.01
  },
  {
    "channel": 3,
    "zrepcode": "789789",
    "pipeline": 0
  },
  {
    "channel": 3,
    "zrepcode": "789789",
    "inv_bal": 341
  }
]

Sample output:

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

[
{'channel': 1, 'zrepcode': '123456', 'turn': 7833.9, 'pipeline': 324.0,'display': 341,'inv_bal': 941.16},
{'channel': 3, 'zrepcode': '123456', 'turn': 7935.01, 'pipeline': 0.0, 'display': 941.16, 'inv_bal': 341.0},
{'channel': 3, 'zrepcode': '789789', 'turn': 7935.01, 'pipeline': 0.0, 'display': 941.16, 'inv_bal': 341.0}
]

>Solution :

Easily solved with our good friend collections.defaultdict:

import collections


by_key = collections.defaultdict(dict)

for datum in data:  # data is the list of dicts from the post
    key = (datum.get("channel"), datum.get("zrepcode"))  # form the key tuple
    by_key[key].update(datum)  # update the defaultdict by the key tuple

print(list(by_key.values()))

This outputs

[
  {'channel': 1, 'zrepcode': '123456', 'turn': 7833.9, 'pipeline': 324, 'inv_bal': 941.16, 'display': 341},
  {'channel': 3, 'zrepcode': '123456', 'display': 941.16, 'turn': 7935.01, 'pipeline': 0, 'inv_bal': 341},
  {'channel': 3, 'zrepcode': '789789', 'display': 941.16, 'turn': 7935.01, 'pipeline': 0, 'inv_bal': 341},
]
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