Home > Blockchain >  Combine duplicates inside a list of dicts
Combine duplicates inside a list of dicts

Time:02-13

How do I combine the values of the duplicates keys of the dictionary inside of this list?

lst = [{'Crossroads Dashboard Group': 'Secure Servers', 'InstanceList': ['i-02401438d45214e92']}, 
{'Crossroads Dashboard Group': 'IPA Servers', 'InstanceList': ['i-077ac3525ee8a8c5f']}, 
{'Crossroads Dashboard Group': 'Secure Servers', 'InstanceList': ['i-0d8c63992972b52bc']}]

Desired outcome is:

lst = [
{'Crossroads Dashboard Group': 'Secure Servers', 'InstanceList': ['i-02401438d45214e92', 'i-0d8c63992972b52bc']}, 
{'Crossroads Dashboard Group': 'IPA Servers', 'InstanceList': ['i-077ac3525ee8a8c5f']}]

CodePudding user response:

First, collect into a dict of which uses (consistently ordered) tuples to store hashable data. Then, convert that to the desired representation.

from collections import defaultdict

def combine(ds):
    instances = defaultdict(list)

    for d in ds:
        key = tuple((k, d[k]) for k in sorted(d.keys()) if k != "InstanceList")
        instances[key].extend(d["InstanceList"])

    return [{**dict(k), "InstanceList": v} for k, v in instances.items()]

Test:

ds = [
    {"server": "A", "InstanceList": ["A1", "A2"]},
    {"server": "A", "InstanceList": ["A3", "A4"]},
    {"server": "B", "InstanceList": ["B1", "B2"]},
]

>>> combine(ds)
[
    {'server': 'A', 'InstanceList': ['A1', 'A2', 'A3', 'A4']},
    {'server': 'B', 'InstanceList': ['B1', 'B2']},
]
  • Related