Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two Lists to Json Format in python

I have two lists

a=["USA","France","Italy"]
b=["10","5","6"]

I want the end result to be in json like this.

[{"country":"USA","wins":"10"},
{"country":"France","wins":"5"},
{"country":"Italy","wins":"6"},
]

I used zip(a,b) to join two but couldn't name it

like image 765
Dexter Stack Avatar asked Aug 17 '14 11:08

Dexter Stack


1 Answers

You first have to set it up as a list, and then add the items to it

import json

jsonList = []
a=["USA","France","Italy"]
b=["10","5","6"]

for i in range(0,len(a)):
    jsonList.append({"country" : a[i], "wins" : b[i]})


print(json.dumps(jsonList, indent = 1))
like image 93
ruben1691 Avatar answered Sep 22 '22 11:09

ruben1691