Home > Software design >  Adding a new element to a nested dictionary in python
Adding a new element to a nested dictionary in python

Time:05-27

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.

    [ 
      {
        "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)

CodePudding user response:

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}}}]

CodePudding user response:

You should be able add it the way you would add any other key / value pair:

def add_to_inner(new_key, new_val):
    for key, value in d.items():
        if isinstance(value, dict):
            add_to_inner(new_key, new_val)
        else:
            value[new_key] = new_val
  • Related