Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python | How to create complex dictionary

Tags:

python

I want to create a data structure that will be parse as a JSON object. The out put must look like this and this should be a dynamic data structure.

{"data": [{"type": "locale", "lat": -34.43778387240597, "lon": 150.04799169921876},
{"type": "poi", "lat": -34.96615974838191, "lon": 149.89967626953126},
{"type": "locale", "lat": -34.72271328279892, "lon": 150.46547216796876},
{"type": "poi", "lat": -34.67303411621243, "lon": 149.96559423828126}]}

I'm struggling in the middle of implementing this data structure so expecting some good ideas.

Thanks

like image 300
Switch Avatar asked Dec 28 '22 13:12

Switch


1 Answers

As response to the comment on Mathiasdm answer: You mean how to create dictionary with a list of dictionaries? That can be done like this:

dict = {}
dict["data"] = []
dict["data"].append({'type': 'poi', 'lat': 123})
dict["data"].append({'type': 'locale', 'lat': 321})

And so on.

But if this was really the problem, i would suggest to read the reference for lists and dictionaries again: http://docs.python.org/tutorial/datastructures.html

like image 119
enricog Avatar answered Jan 09 '23 17:01

enricog