Python Pretty Print List of Dictionaries Example
Hello Dev,
I am going to show you an example of python pretty print list of dictionaries. This example will help you python pretty print dictionary as json. you will learn python dictionary pretty print. I explained simply about python pretty print nested dictionary. Here, Create a basic example of python pretty print list example.
In you want to display dictionary in proper format from string in python then i will give you two examples to do pretty print list of dictionaries in python language. first we will use json.dumps() and second one will use pprint.pprint() function. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.
Example 1: Python Pretty Print Dictionary using json.dumps()
main.py
import json # Create New Json myList = [{"id": 1,"name": "Hardik"}, {"id": 2,"name": "Vimal"}, {"id": 3,"name": "Harshad"}] # Display JSON with Prettyprint print(json.dumps(myList, sort_keys=False, indent=4))
Output:
[ { "id": 1, "name": "Hardik" }, { "id": 2, "name": "Vimal" }, { "id": 3, "name": "Harshad" } ]
Example 2: Python Pretty Print Dictionary using pprint.pprint()
main.py
import pprint # Create New Json myList = [{"id": 1,"name": "Hardik"}, {"id": 2,"name": "Vimal"}, {"id": 3,"name": "Harshad"}] # Display JSON with Prettyprint pprint.pprint(myList)
Output:
[{'id': 1, 'name': 'Hardik'}, {'id': 2, 'name': 'Vimal'}, {'id': 3, 'name': 'Harshad'}]
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get First Value in Dictionary Python?
- How to Get First Key in Dictionary Python?
- Python Dictionary Convert Values to Keys Example
- How to Add Multiple Elements to a List in Python?
- How to Add Element to a List in Python?
- Python Convert List into String with Commas Example
- How to Convert List into String in Python?
- How to Get Unique Elements from List in Python?
- Python Delete Files Matching Pattern Example
- Python Post Request with Basic Authentication Example
- How to Check if Today is Tuesday or not in Python?
- How to Add Seconds to DateTime in Python?