cumhur
cumhur

Reputation: 3

Python Merging multiple dictionaries

I have a list as follows

 items = [{'Item Shortname': 'tshirt', 'Skins': '10053'},
 {'Item Shortname': 'tshirt', 'Skins': '10039'},
 {'Item Shortname': 'tshirt', 'Skins': '584379'},
 {'Item Shortname': 'tshirt', 'Skins': '10043'},
 {'Item Shortname': 'vending.machine', 'Skins': '861029759'},
 {'Item Shortname': 'vending.machine', 'Skins': '862137836'},
 {'Item Shortname': 'vending.machine', 'Skins': '869474635'},
 {'Item Shortname': 'water.purifier', 'Skins': '886677071'},
 {'Item Shortname': 'water.purifier', 'Skins': '786826476'}]

How do I generate an output like this:

{
  {'Item Shortname': 'tshirt', 'Skins': [0, 10053, 10039, 584379, 10043]},
  {'Item Shortname': 'vending.machine', 'Skins': [0, 861029759, 862137836, 869474635]},
  {'Item Shortname': 'water.purifier', 'Skins': [0, 886677071, 786826476]}
}

I have tried this with the default dict https://docs.python.org/3/library/collections.html#collections.defaultdict but couldn't figured out.

Upvotes: 0

Views: 54

Answers (2)

azro
azro

Reputation: 54148

You can easily do it with the default dict, but you need an intermediate step, when you use the ItemShortname as key

result = collections.defaultdict(list)
for item in items:
    result[item['Item Shortname']].append(int(item['Skins']))
print(result)  # {"tshirt": ["10053", "10039", "584379", "10043"], "vending.machine": ["861029759", "862137836", "869474635"], "water.purifier": ["886677071", "786826476"]}

result = [{'Item Shortname': k, 'Skins': v} for k, v in result.items()]
print(result)  # [{'Item Shortname': 'tshirt', 'Skins': [10053, 10039, 584379, 10043]}, {'Item Shortname': 'vending.machine', 'Skins': [861029759, 862137836, 869474635]}, {'Item Shortname': 'water.purifier', 'Skins': [886677071, 786826476]}]

Other way doing mostly the same but with groupby

values = [list(item.values()) for item in items]
result = groupby(values, lambda x: x[0]) # from itertools import groupby
result = [{'Item Shortname': k, 'Skins': [int(x[1]) for x in v]} for k, v in result]

Giving the

[
    {
        "Item Shortname": "tshirt",
        "Skins": [10053,10039,584379,10043]
    },
    {
        "Item Shortname": "vending.machine",
        "Skins": [861029759,862137836,869474635]
    },
    {
        "Item Shortname": "water.purifier",
        "Skins": [886677071,786826476]
    }
]

Upvotes: 1

Erich
Erich

Reputation: 1848

Use a list instead of a dictionary for the final result. Then you can iterate over the initial items list and build the result iteratively.

summary = []

for item in items:
    name = item['Item Shortname']
    added = False
    # try to find a subnet with given address
    for elem in summary:
        if elem['Item Shortname'] == name:
            # if found, add domain to list
            elem['Skins'].append(int(item['Skins']))
            # and remember that we found a matching subnet
            added = True
            break
    if not added:
        # if we didn't find any subnet, add a new one
        summary.append({'Item Shortname': name,
                        'Skins': [ 0, int(item['Skins']) ] })

Upvotes: 1

Related Questions