Reputation: 2513
I'm in the process of generating a json file from python, however, i'd like separate the dictionaries via a comma after each loop, here's a portion of the code:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"targert": {
"item": item
}
},
The desired output should be:
[
{
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
},
{
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}
]
So basically a comma right after the first end brace of the first loop: }, and all in one list, one bracket on top and one closing bracket at the bottom.
When running the code, it doesn't show the comma after each brace in the loop, and it does input each loop within a list automatically, any suggestions ?
What I get:
[ {
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
}
]
[ {
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}]
Upvotes: 0
Views: 339
Reputation: 317
I think this is what your looking for:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
mylist = []
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"target": {
"item": item
}
}
mylist.append(mydict)
print(mylist)
Upvotes: 1