How to Merge Two Two Items in Dictionary Python

Question:

I want to put two items of the dictionary into one entity in respective keys if idx=… is similar in keys then all the comparable data with matching keys becomes one item in dict with 0, 1, … keys see below demo data to understand, I learned this page but I’m not able to do it if you know how to do it, please help me

Demo Data:

{
    "system": {
        "camera[idx=0]": {
            "fps": {
                "value": 24,
                "xpath": "/system/camera[idx=0]/fps",
                "index": 3,
                "string": "fps",
                "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=0]"],
            },
        },
        "motion_detection[idx=0]": {
            "threshold_type": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                "index": 5,
                "string": "threshold_type",
                "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=0]",
                ],
            },
        },
        "camera[idx=1]": {
            "vendor_name": {
                "value": "Raspberry",
                "xpath": "/system/camera[idx=1]/vendor_name",
                "index": 3,
                "string": "vendor_name",
                "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=1]"],
            }
        },
        "motion_detection[idx=1]": {
            "threshold_min": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                "index": 5,
                "string": "threshold_min",
                "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=1]",
                ],
            },
        },
    }
}

expecting data

{
    "system": {
        "0": {
            "camera": {
                "fps": {
                    "value": 24,
                    "xpath": "/system/camera[idx=0]/fps",
                    "index": 3,
                    "string": "fps",
                    "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=0]"],
                },
            },
            "motion_detection": {
                "threshold_type": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                    "index": 5,
                    "string": "threshold_type",
                    "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=0]",
                    ]
                }
            }
        },
        "1": {
            "camera": {
                "vendor_name": {
                    "value": "Raspberry",
                    "xpath": "/system/camera[idx=1]/vendor_name",
                    "index": 3,
                    "string": "vendor_name",
                    "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=1]"]
                }
            },
            "motion_detection": {
                "threshold_min": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                    "index": 5,
                    "string": "threshold_min",
                    "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=1]",
                    ]
                }
            }
        }
    }
}
Asked By: Jocefyneroot

||

Answers:

Here is my best attempt at solving your problem – I effectively broke the problem down into a couple steps:

  1. Get the key you want to parse (e.g. the camera[idx=0]).
  2. Extract both the leading label and the value of the idx (in the above case, camera and 0).

If you solve those two points, the rest is fairly easy to figure out.


I solved the problem by relying on a little bit of regex. Here is my solution:

import re

data = ...  # This is the input data structure you supplied in your example. Omitted for brevity.

outputs = {}

for key, value in data["system"].items():
    label, idx = re.search(r"(.+)[idx=(.+)]", key).groups()

    if idx not in outputs:
        outputs[idx] = {}

    outputs[idx][label] = {**value, **value}

outputs = {"system": outputs}  # Restructure according to required spec.
print(outputs)

This gave me the desired output. Hope that helps! 🙂

Answered By: K.L.Miller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.