Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active January 1, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/0d1b5b0582c39959ed4045b0aea26aa9 to your computer and use it in GitHub Desktop.
Save hygull/0d1b5b0582c39959ed4045b0aea26aa9 to your computer and use it in GitHub Desktop.
Stackoverflow: appned, update, dictionary

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"
	        ]
	    }
	}
	>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment