Updating nested dict list keys - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Updating nested dict list keys (/thread-36337.html) |
Updating nested dict list keys - tbaror - Feb-09-2022 Hello , I am trying to update values in in dict that construct from nested dict and list the dict as follows dict_file ={'job_name': 'system', 'static_configs': [{'targets': ['localhost'], 'labels': {'job': 'varlogs', '__path__': '/var/log/*log', 'host': 'grafana'}}]}I can get the values of the desired keys as shown below but don't know how to update them job_name = dict_file.get('job_name') host_name = dict_file.get('static_configs')[0].get('labels').get('host') path_name = dict_file.get('static_configs')[0].get('labels').get('__path__')Please advice Thanks RE: Updating nested dict list keys - ibreeden - Feb-09-2022 >>> dict_file = {'job_name': 'system', 'static_configs': [{'targets': ['localhost'], 'labels': {'job': 'varlogs', '__path__': '/var/log/*log', 'host': 'grafana'}}]} >>> dict_file['job_name'] 'system' >>> dict_file['static_configs'][0]['labels']['host'] 'grafana' >>> dict_file['static_configs'][0]['labels']['__path__'] '/var/log/*log' >>> dict_file['job_name'] = 'my_job' >>> dict_file['static_configs'][0]['labels']['host'] = 'my_host' >>> dict_file['static_configs'][0]['labels']['__path__'] = '/my_path' >>> dict_file['job_name'] 'my_job' >>> dict_file['static_configs'][0]['labels']['host'] 'my_host' >>> dict_file['static_configs'][0]['labels']['__path__'] '/my_path' RE: Updating nested dict list keys - tbaror - Feb-09-2022 Thank you |