473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. properties = {
  2.  
  3. 'livenessTests': [
  4.  
  5. {
  6.  
  7. 'name':'http' + '_' + 'livenesstest',
  8.  
  9. 'testObject':'/default.html'
  10.  
  11. },
  12.  
  13. {
  14.  
  15. 'name':'https' + '_' + 'livenesstest',
  16.  
  17. 'testObject':'/default.html'
  18.  
  19. }
  20. ]
  21.  
The resulting "json" directory should be something like

Expand|Select|Wrap|Line Numbers
  1. json : {
  2.  
  3. "acg": {
  4.  
  5. "id": "1-7KLGU.G19717",
  6.  
  7. "name": "Akamai Internal-1-7KLGU - 1-7KLGU.G19717"
  8.  
  9. },
  10.  
  11. "asmappings": [],
  12.  
  13. "cidrMaps": [],
  14.  
  15. "properties": {"livenessTests" : [{<contents from list above>},
  16.                                   {<contents from list above>}
  17.                                  ] 
  18.  
  19.               }
I am trying this :


Expand|Select|Wrap|Line Numbers
  1. json['properties'] = []  
  2.  
  3. for key in properties.keys():
  4.     print key
  5.     json['properties'].append(property[key])
  6.  
But I am getting ,

Expand|Select|Wrap|Line Numbers
  1.  "livenessTests": [
  2.      {
  3.          "name": "http_livenessTest",
  4.          "testObject": "/default.html"
  5.      },
  6.      {
  7.          "name": "https_livenessTest",
  8.          "testObject": "/default.html"
  9.      },
so I am not getting the key 'properties' .
Can you tell me where I am wrong ?
Mar 11 '19 #1
1 1075
sritaa
5
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.
Mar 13 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ruari mactaggart | last post by:
can this be tidied up ? >>>verb={} >>>verb=,, ,''],,,] it is to create a dictionary entry with six empty lists of six items as values. or is it unnecesary ? They represent six tenses with...
8
by: beliavsky | last post by:
Since Python does not have declarations, I wonder if people think it is good to name function arguments according to the type of data structure expected, with names like "xlist" or "xdict".
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
9
by: javuchi | last post by:
I've been searching thru the library documentation, and this is the best code I can produce for this alogorithm: I'd like to return a dictionary which is a copy of 'another' dictionary whoes...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
4
by: Nico Grubert | last post by:
Hi there, I am looking for a way to sort a list containing dictionaries. This is my example list: I want to sort the list by dictionary's key 'from_datetime' so the sorted list should be:
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
4
GTXY20
by: GTXY20 | last post by:
Hello; I have the following dictionary: d={1:, 2:, 3:, 4:} I am having trouble removing the duplicate values in each of the keys value list, I need it so that: d={1:, 2:, 3:, 4:}{/CODE]
10
by: ++imanshu | last post by:
Hi, Wouldn't it be nicer to have 'in' return values (or keys) for both arrays and dictionaries. Arrays and Dictionaries looked so similar in Python until I learned this difference. Thanks,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.