Python Forum
Updating nested dict list keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating nested dict list keys
#1
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
Reply
#2
>>> 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'
tbaror likes this post
Reply
#3
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating Code And Having Issue With Keys Xileron 8 1,158 May-25-2023, 11:14 PM
Last Post: DigiGod
  List all possibilities of a nested-list by flattened lists sparkt 1 919 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,211 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,456 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Loop Dict with inconsistent Keys Personne 1 1,610 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,911 Jan-23-2022, 07:20 PM
Last Post: menator01
  Remove empty keys in a python list python_student 7 3,029 Jan-12-2022, 10:23 PM
Last Post: python_student
  Python 3.8 Nested varible not updating Teknohead23 6 2,429 Oct-02-2021, 11:49 AM
Last Post: Teknohead23
  changing key names in nested dict wardancer84 6 2,144 Sep-10-2021, 08:13 AM
Last Post: wardancer84
  Looping through nested elements and updating the original list Alex_James 3 2,126 Aug-19-2021, 12:05 PM
Last Post: Alex_James

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020