Home > Net >  How to merge list of dictionaries by unique key value
How to merge list of dictionaries by unique key value

Time:01-01

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:

[
{'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}
]

CodePudding user response:

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},
]
  • Related