two Lists to Json Format in python
Question:
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
Answers:
Using list comprehension:
>>> [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
[{'country': 'USA', 'wins': '10'},
{'country': 'France', 'wins': '5'},
{'country': 'Italy', 'wins': '6'}]
Use json.dumps
to get JSON:
>>> json.dumps(
... [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
... )
'[{"country": "USA", "wins": "10"}, {"country": "France", "wins": "5"}, {"country": "Italy", "wins": "6"}]'
You can combine map
with zip
.
jsonized = map(lambda item: {'country':item[0], 'wins':item[1]}, zip(a,b))
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))
In addition to the answer of ‘falsetru’ if you need an actual json object (and not only a string with the structure of a json) you can use json.loads()
and use as parameter the string that json.dumps()
outputs.
also for just combine two list to json format:
def make_json_from_two_list():
keys = ["USA","France","Italy"]
value = ["10","5","6"]
jsons = {}
x = 0
for item in keys:
jsons[item[0]] = value[x]
x += 1
return jsons
print(ake_json_from_two_list())
result>>>> {"USA":"10","France":"5","Italy":"6"}
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
Using list comprehension:
>>> [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
[{'country': 'USA', 'wins': '10'},
{'country': 'France', 'wins': '5'},
{'country': 'Italy', 'wins': '6'}]
Use json.dumps
to get JSON:
>>> json.dumps(
... [{'country': country, 'wins': wins} for country, wins in zip(a, b)]
... )
'[{"country": "USA", "wins": "10"}, {"country": "France", "wins": "5"}, {"country": "Italy", "wins": "6"}]'
You can combine map
with zip
.
jsonized = map(lambda item: {'country':item[0], 'wins':item[1]}, zip(a,b))
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))
In addition to the answer of ‘falsetru’ if you need an actual json object (and not only a string with the structure of a json) you can use json.loads()
and use as parameter the string that json.dumps()
outputs.
also for just combine two list to json format:
def make_json_from_two_list():
keys = ["USA","France","Italy"]
value = ["10","5","6"]
jsons = {}
x = 0
for item in keys:
jsons[item[0]] = value[x]
x += 1
return jsons
print(ake_json_from_two_list())
result>>>> {"USA":"10","France":"5","Italy":"6"}