close

How to pretty Print a Dictionary in Python

Hi guys. How are you all? Today in this tutorial we will learn about How to pretty Print a Dictionary in Python. Here i am giving you some methods which will be seful for you to pretty print a dictionary in python. This will be more easy to readable format. So this methods converts a dictionary to the more readable format. So lets learn about of this by given methods. I hope you all like it.

How to pretty Print a Dictionary in Python

  1. Pretty Print a Dictionary in Python

    to pretty Print a Dictionary in Python just Use print(). You can use pprint() function for to pretty print a dictionary in python. For this you have to import the pprint first. After it make your dictarray and then use the pprint function with it. It will pretty print your dictionary in a readable format or style. So lets learn this method by given below example. I hope you all like it. import pprint dctarr = [ {'Name': 'Jiya', 'standard': '11', 'percentage': '98'}, {'Name': 'Jeny', 'standard': '12', 'percentage': '80'}, {'Name': 'Reny', 'standard': '12', 'percentage': '95'}, {'Name': 'Piya', 'standard': '11', 'percentage': '89'} ] pprint.pprint(dctarr) Output : [{'Name': 'Jiya', 'percentage': '98', 'standard': '11'}, {'Name': 'Jeny', 'percentage': '80', 'standard': '12'}, {'Name': 'Reny', 'percentage': '95', 'standard': '12'}, {'Name': 'Piya', 'percentage': '89', 'standard': '11'}]

  2. How to pretty Print a Dictionary in Python

    to pretty Print a Dictionary in Python just Use json.dumps(). By using json.dumps() you can pretty print a dictionary. Here we will import json first and then make our dictarray. This method is quite similar to above method. Then we will use json.dumps() function and will print it. This will give the output in a readable format. So lets learn this by given below example. import json dctarr = [ {'Name': 'Jiya', 'standard': '11', 'percentage': '98'}, {'Name': 'Jeny', 'standard': '12', 'percentage': '80'}, {'Name': 'Reny', 'standard': '12', 'percentage': '95'}, {'Name': 'Piya', 'standard': '11', 'percentage': '89'} ] print(json.dumps(dctarr, sort_keys=False, indent=4)) Output : [ { "Name": "Jiya", "standard": "11", "percentage": "98" }, { "Name": "Jeny", "standard": "12", "percentage": "80" }, { "Name": "Reny", "standard": "12", "percentage": "95" }, { "Name": "Piya", "standard": "11", "percentage": "89" } ]

Method 1: Use print()

You can use pprint() function for to pretty print a dictionary in python. For this you have to import the pprint first. After it make your dictarray and then use the pprint function with it. It will pretty print your dictionary in a readable format or style. So lets learn this method by given below example. I hope you all like it.

import pprint
dctarr = [
  {'Name': 'Jiya', 'standard': '11', 'percentage': '98'},
  {'Name': 'Jeny', 'standard': '12', 'percentage': '80'},
  {'Name': 'Reny', 'standard': '12', 'percentage': '95'},
  {'Name': 'Piya', 'standard': '11', 'percentage': '89'}
]
pprint.pprint(dctarr)

Output :

[{'Name': 'Jiya', 'percentage': '98', 'standard': '11'},
 {'Name': 'Jeny', 'percentage': '80', 'standard': '12'},
 {'Name': 'Reny', 'percentage': '95', 'standard': '12'},
 {'Name': 'Piya', 'percentage': '89', 'standard': '11'}]

Method 2: Use json.dumps()

By using json.dumps() you can pretty print a dictionary. Here we will import json first and then make our dictarray. This method is quite similar to above method. Then we will use json.dumps() function and will print it. This will give the output in a readable format. So lets learn this by given below example.

import json
dctarr = [
  {'Name': 'Jiya', 'standard': '11', 'percentage': '98'},
  {'Name': 'Jeny', 'standard': '12', 'percentage': '80'},
  {'Name': 'Reny', 'standard': '12', 'percentage': '95'},
  {'Name': 'Piya', 'standard': '11', 'percentage': '89'}
]
print(json.dumps(dctarr, sort_keys=False, indent=4))

Output :

[
    {
        "Name": "Jiya",
        "standard": "11",
        "percentage": "98"
    },
    {
        "Name": "Jeny",
        "standard": "12",
        "percentage": "80"
    },
    {
        "Name": "Reny",
        "standard": "12",
        "percentage": "95"
    },
    {
        "Name": "Piya",
        "standard": "11",
        "percentage": "89"
    }
]

Method 3: Use yaml.dumps()

Here we will use yaml.dumps() function to pretty print a dictionary in python. First of all import yaml and then make your dictarray and after it use the yaml.dumps() function and then print it. It will give the same output as given in above methods. So by this methods you can get the pretty print dictionary. So lets learn this by given below example:

import yaml
dctarr = [
  {'Name': 'Jiya', 'standard': '11', 'percentage': '98'},
  {'Name': 'Jeny', 'standard': '12', 'percentage': '80'},
  {'Name': 'Reny', 'standard': '12', 'percentage': '95'},
  {'Name': 'Piya', 'standard': '11', 'percentage': '89'}
]
print(yaml.dump(dctarr, sort_keys=False, default_flow_style=False))

Output :

- Name: Jiya
  standard: '11'
  percentage: '98'
- Name: Jeny
  standard: '12'
  percentage: '80'
- Name: Reny
  standard: '12'
  percentage: '95'
- Name: Piya
  standard: '11'
  percentage: '89'

Conclusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment