How to add a lists of dictionaries to another dictionary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blockgeek
    New Member
    • Mar 2019
    • 1

    How to add a lists of dictionaries to another dictionary?

    I have this dictionary "properties " with a key livenessTests and in turn the livenessTests is a list of dictionaries. How do I add/append livenessTests to another dictionary json?

    Code:
    properties = {
    
    'livenessTests': [
    
    {
    
    'name':'http' + '_' + 'livenesstest',
    
    'testObject':'/default.html'
    
    },
    
    {
    
    'name':'https' + '_' + 'livenesstest',
    
    'testObject':'/default.html'
    
    }
    ]
    The resulting "json" directory should be something like

    Code:
    json : {
    
    "acg": {
    
    "id": "1-7KLGU.G19717",
    
    "name": "Akamai Internal-1-7KLGU - 1-7KLGU.G19717"
    
    },
    
    "asmappings": [],
    
    "cidrMaps": [],
    
    "properties": {"livenessTests" : [{<contents from list above>},
                                      {<contents from list above>}
                                     ] 
    
                  }
    I am trying this :


    Code:
    json['properties'] = []  
    
    for key in properties.keys():
        print key
        json['properties'].append(property[key])
    But I am getting ,

    Code:
     "livenessTests": [
         {
             "name": "http_livenessTest",
             "testObject": "/default.html"
         },
         {
             "name": "https_livenessTest",
             "testObject": "/default.html"
         },
    so I am not getting the key 'properties' .
    Can you tell me where I am wrong ?
  • sritaa
    New Member
    • Jan 2019
    • 5

    #2
    The same way you would add any other object.

    myDict = {}
    myDict[“myList”] = []
    That would add an empty list, whose key is “myList”, to the dictionary “myDict”.

    You could also use the dict “update” method, as follows:

    myDict.update( myList: [] )
    or this way:

    myDict.update( {‘myList’: []} )
    Although the second method is more appropriate to add several key/value pairs or for merging two dictionaries.

    Comment

    Working...