Problem link: https://stackoverflow.com/questions/53994537/update-and-append-on-same-time
I think, the following would be helpful for you.
>>> listx = ["apple","mango","pineapple"]
>>> rets = {
... "author": "Jhonny",
... "old": 17,
... "married": False,
... "brother": []
... }
>>>
>>> my_json = {'family': rets}
>>>
>>> for ret in listx:
... my_json['family']['brother'].append(ret)
...
>>> my_json
{'family': {'author': 'Jhonny', 'old': 17, 'married': False, 'brother': ['apple', 'mango', 'pine
apple']}}
>>>
You can print the dictionary beautifully as follows.
>>> import json
>>> print(json.dumps(my_json, indent=4))
{
"family": {
"author": "Jhonny",
"old": 17,
"married": false,
"brother": [
"apple",
"mango",
"pineapple"
]
}
}
>>>