Merge dictionaries in a list on common key-value

Viewed 81

What is the pythonic way to properly merge dictionaries in a list Given list of dictionaries:

[
    {
        "introduced_at": "28.12.2000",
        "item": "Pizza",
        "item_price": [
            {
                "date": "01.01.2011",
                "details": [
                    {"price": 1000, "changed_by": "Dieter"},
                    {"price": 900, "changed_by": "Nina"},
                ],
            }
        ],
    },
    {
        "introduced_at": "28.12.2000",
        "item": "Pizza",
        "item_price": [
            {
                "date": "01.01.2012",
                "details": [
                    {"price": 1050, "changed_by": "Dieter"},
                    {"price": 900, "changed_by": "Nina"},
                ],
            }
        ],
    },
]

expected:

{
    "price_dev": [
        {
            "introduced_at": "28.12.2000",
            "item": "Pizza",
            "item_price": [
                {
                    "date": "01.01.2011",
                    "details": [
                        {"price": 1000, "changed_by": "Dieter"},
                        {"price": 900, "changed_by": "Nina"},
                    ],
                },
                {
                    "date": "01.01.2012",
                    "details": [
                        {"price": 1050, "changed_by": "Dieter"}
                    ],
                },
            ],
        }
    ]
}

The key item and its value in combination with their introduced_at can be found twice in the list so they may be used as unique idenfitiers to merge the entries on. As for the details, since date are both different, they should be displayed seperately. Duplicates as seen in changed_by Nina should be disregarded.

1 Answers

I have tried this. can you check it is correct or not.

dl = [
    {
        "introduced_at": "28.12.2000",
        "item": "Pizza",
        "item_price": [
            {
                "date": "01.01.2011",
                "details": [
                    {"price": 1000, "changed_by": "Dieter"},
                    {"price": 900, "changed_by": "Nina"},
                ],
            }
        ],
    },
    {
        "introduced_at": "28.12.2000",
        "item": "Pizza",
        "item_price": [
            {
                "date": "01.01.2012",
                "details": [
                    {"price": 1050, "changed_by": "Dieter"},
                    {"price": 900, "changed_by": "Nina"},
                ],
            }
        ],
    },
]
ans_dict = {}
pre_list = []
for data in dl:
    if not pre_list:
        pre_list.append(data)
    else:
        for items, index in zip(pre_list, range(len(pre_list))):
            if items['introduced_at'] == data['introduced_at'] and items['item'] == data['item']:
                for item_data in data['item_price']:
                    for old_items in items['item_price']:
                        if item_data['date'] != old_items['date']:
                            pairs = zip(item_data['details'], old_items['details'])
                            add_dic = [x for x, y in pairs if x != y]
                    items['item_price'].append({"date": item_data['date'], "details": add_dic})

ans_dict["price_dev"] = pre_list
print(ans_dict)

output:

{
  'price_dev': [
    {
      'introduced_at': '28.12.2000',
      'item': 'Pizza',
      'item_price': [
        {
          'date': '01.01.2011',
          'details': [
            {
              'price': 1000,
              'changed_by': 'Dieter'
            },
            {
              'price': 900,
              'changed_by': 'Nina'
            }
          ]
        },
        {
          'date': '01.01.2012',
          'details': [
            {
              'price': 1050,
              'changed_by': 'Dieter'
            }
          ]
        }
      ]
    }
  ]
}
Related