Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a new element to a nested dictionary in python

I have a list nested dictionary that looks in the following way and I have an epoch timestamp stored as a variable.

ts = 1653070640

   [ 
      {
        "HR": {
            "EKG": {
                "HR_EKG": 136.0
            },
            "PPG": {
                "HR_PPG_1": 135.0,
                "HR_PULSED": 135.0
            }
        },
        "NIBP": {
            "DBP": {
                "NIBPD": 25.0
            },
            "MBP": {
                "NIBPM": 53.0
            },
            "SBP": {
                "NIBPS": 73.0
            }
        },
        "SPO2": {
            "PI": {
                "PI_1": 4.25
            },
            "SPO2": {
                "SpO2_1": 86.0
            }
        }
    } 
 ]

What I want to do is to add the ts variable as a new key to the innermost nested dictionary. That nested dictionary length may vary so I guess it should be a recursive approach.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    [ 
      {
        "HR": {
            "EKG": {
                "HR_EKG": 136.0
                "timestamp": 1653070640

            },
            "PPG": {
                "HR_PPG_1": 135.0,
                "HR_PULSED": 135.0,
                "timestamp": 1653070640
            }
        },
        "NIBP": {
            "DBP": {
                "NIBPD": 25.0,
                "timestamp": 1653070640
            },
            "MBP": {
                "NIBPM": 53.0,
                "timestamp": 1653070640
            },
            "SBP": {
                "NIBPS": 73.0,
                "timestamp": 1653070640
            }
        },
        "SPO2": {
            "PI": {
                "PI_1": 4.25,
                "timestamp": 1653070640
            },
            "SPO2": {
                "SpO2_1": 86.0,
                "timestamp": 1653070640
            }
        }
    }  
]

What I’ve tried is the following function that prints all the keys but not sure how to add a new key to the inner most dict.

def get_all_keys(d):
  for key, value in d.items():
     yield key
     if isinstance(value, dict):
        yield from get_all_keys(value)

>Solution :

You can do this to add timestamp only at depths where there are no sub-dicts:

def set_ts(d, ts):
    any_nested = False
    for k, v in d.items():
        if isinstance(v, dict):
            set_ts(v, ts)
            any_nested = True
    if not any_nested:
        d['timestamp'] = ts

Usage:

for d in dl:
    set_ts(d, ts)

>>> dl
[{'HR': {'EKG': {'HR_EKG': 136.0, 'timestamp': 1653070640},
   'PPG': {'HR_PPG_1': 135.0, 'HR_PULSED': 135.0, 'timestamp': 1653070640}},
  'NIBP': {'DBP': {'NIBPD': 25.0, 'timestamp': 1653070640},
   'MBP': {'NIBPM': 53.0, 'timestamp': 1653070640},
   'SBP': {'NIBPS': 73.0, 'timestamp': 1653070640}},
  'SPO2': {'PI': {'PI_1': 4.25, 'timestamp': 1653070640},
   'SPO2': {'SpO2_1': 86.0, 'timestamp': 1653070640}}}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading