Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to pretty Print a Dictionary in Python. so without wasting time lets learn about of this.
How to pretty Print a Dictionary in Python
- pretty Print a Dictionary in Python
to pretty Print a Dictionary in Python just Use pprint().By using pprint() you can pretty print a dictionary. So without wasting time lets learn about of this by given below example:
import pprint arrdct1 = [{'name':'aarti','subject':'physics','marks':'86'}, {'name':'trusha','subject':'chemistry','marks':'98'}, {'name':'meet','subject':'maths','marks':'78'}, {'name':'rohan','subject':'biology','marks':'92'} ] pprint.pprint(arrdct1)
Output :[{'marks': '86', 'name': 'aarti', 'subject': 'physics'}, {'marks': '98', 'name': 'trusha', 'subject': 'chemistry'}, {'marks': '78', 'name': 'meet', 'subject': 'maths'}, {'marks': '92', 'name': 'rohan', 'subject': 'biology'}]
- How to pretty Print a Dictionary in Python
to pretty Print a Dictionary in Python just Use json.dumps().By using json.dumps() it converts string into json string and json formate. So by this you can pretty print a dictionary. So without wasting time lets learn about of this by given below example:
import json arrdct1 = [ {'name':'aarti','subject':'physics','marks':'86'}, {'name':'trusha','subject':'chemistry','marks':'98'}, {'name':'meet','subject':'maths','marks':'78'}, {'name':'rohan','subject':'biology','marks':'92'} ] print(json.dumps(arrdct1, sort_keys=False, indent=4))
Output :[ { "name": "aarti", "subject": "physics", "marks": "86" }, { "name": "trusha", "subject": "chemistry", "marks": "98" }, { "name": "meet", "subject": "maths", "marks": "78" }, { "name": "rohan", "subject": "biology", "marks": "92" } ]
- python print dictionary pretty
To pretty Print a Dictionary in Python just Use json.dumps().By using json.dumps() it converts string into json string and json formate. So by this you can pretty print a dictionary.
Method 1: Use pprint()
By using pprint() you can pretty print a dictionary. So without wasting time lets learn about of this by given below example:
import pprint
arrdct1 = [
{'name':'aarti','subject':'physics','marks':'86'},
{'name':'trusha','subject':'chemistry','marks':'98'},
{'name':'meet','subject':'maths','marks':'78'},
{'name':'rohan','subject':'biology','marks':'92'}
]
pprint.pprint(arrdct1)
Output :
[{'marks': '86', 'name': 'aarti', 'subject': 'physics'},
{'marks': '98', 'name': 'trusha', 'subject': 'chemistry'},
{'marks': '78', 'name': 'meet', 'subject': 'maths'},
{'marks': '92', 'name': 'rohan', 'subject': 'biology'}]
Method 2: Use json.dumps()
By using json.dumps() it converts string into json string and json formate. So by this you can pretty print a dictionary. So without wasting time lets learn about of this by given below example:
import json
arrdct1 = [
{'name':'aarti','subject':'physics','marks':'86'},
{'name':'trusha','subject':'chemistry','marks':'98'},
{'name':'meet','subject':'maths','marks':'78'},
{'name':'rohan','subject':'biology','marks':'92'}
]
print(json.dumps(arrdct1, sort_keys=False, indent=4))
Output :
[
{
"name": "aarti",
"subject": "physics",
"marks": "86"
},
{
"name": "trusha",
"subject": "chemistry",
"marks": "98"
},
{
"name": "meet",
"subject": "maths",
"marks": "78"
},
{
"name": "rohan",
"subject": "biology",
"marks": "92"
}
]
Conclusion
It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?
Also, Read