Question
My Python app needs to generate a JSON file that will be consumed by React. The Python app generates lists similar to the following:
animals = ["dog","cat","fish"]
count = [3,6,1]
What have I tried
I can dump this to JSON using zip
and a dict
:
json.dumps(dict(zip(animals,count)))
'{"dog": 3, "cat": 6, "fish": 1}'
Desired output
I would like the JSON to look like this:
'[{"name":"dog","count":3},{"name":"cat","count":6},{"name":"fish","count":1}]'
What is the most Pythonic way of doing this?