0

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.

cumhur
  • 3
  • 2
  • Does this answer your question? [How to merge dictionaries of dictionaries?](https://stackoverflow.com/questions/7204805/how-to-merge-dictionaries-of-dictionaries) – MohitC Apr 23 '20 at 21:02

2 Answers2

1

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']) ] })
Erich
  • 1,838
  • 16
  • 20
1

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]
    }
]
azro
  • 53,056
  • 7
  • 34
  • 70