Skip to content
DevEnum.com
en English
ar العربيةzh-CN 简体中文zh-TW 繁體中文en Englishfr Françaisde Deutschit Italianoja 日本語pt Portuguêsru Русскийes Español
  • Contact Us
  • About Us
  • Terms of use
  • Disclaimer
  • Home
  • Python
  • NumPy
  • Pandas
  • React JS
  • C++
  • C
  • JavaScript
  • Rust
  • .Net/C#
  • Cyber-Security
  • Interview Questions
    • Python
    • JavaScript
    • SQL SERVER
    • React
  • Privacy Policy
en English
ar العربيةzh-CN 简体中文zh-TW 繁體中文en Englishfr Françaisde Deutschit Italianoja 日本語pt Portuguêsru Русскийes Español
DevEnum.com
  • About Us
  • C Tutorial
  • C# Tutorial
  • C++ Tutorial
  • Contact Us
  • Cyber-Security
  • Disclaimer
  • Home
  • Interview Questions
  • JavaScript
  • NumPy step by step tutorial
  • Pandas
  • Privacy Policy
  • Python
  • React JS
  • Rust
  • Terms of use
  • Top 90 Javascript Interview Questions and answers

Print nested dictionary line by line in Python(5ways)

Dictionary

In this post, we are going to learn how to Print nested dictionaries line by line in Python using different examples. Python dictionary contains data in form of key-value pair. Where key must be unique but values can be repeated.

1. Print nested dictionary line by line in Python using For loop


The Python dict.items() allow us to iterate over the key-value pair of the dictionary. We have used dict.items() method with the nested for loop to iterate over the nested dictionary.

  • The first loop we are using to print the name of the nested dictionary
  • The second loop is to iterate over the key-value pair of the dictionary nested dictionary
  • The print statement is to print nested dictionary.
data_dict ={'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'},
'Record4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}


for key, value in data_dict.items():
    print('\n',key+':\n')
    for key, value in value.items():
        print('{} : {}'.format(key, value))

Output

 Record1:

Name : Jack
Mark : 100
Subject : Math

 Record2:

Name : Rack
Mark : 100
Subject: : Math

 Record3:

Name : Max
Mark : 100
Subject : Music

 Record4:

Name : David
Mark : 100
Subject : Math


2. Print nested Dictionary using YAML Module


We can use, The YAML module to pretty print a nested dictionary. To use this module, first, we have to install it on the system. The pip command to install it is “pip install PyYAML” and the command to import this module is “import yaml”.

The yaml.dump() method is used to print the nested dictionary by passing the name of the dictionary as a parameter.

import yaml


data_dict ={'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'},
'Record4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}

print(yaml.dump(data_dict))

Output

 Record1:

Name : Jack
Mark : 100
Subject : Math

 Record2:

Name : Rack
Mark : 100
Subject: : Math

 Record3:

Name : Max
Mark : 100
Subject : Music

 Record4:

Name : David
Mark : 100
Subject : Math


3. Print nested dictionary using pprint Module


The pprint module is specially used to “pretty-print” Python data structure. We can install it using this command “pip install pprintpp” and import it using “import pprint”.The pprint module pprint() method is used to print a nested dictionary.

import pprint

data_dict ={'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'},
'Record4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}

pprint.pprint(data_dict , width=1)

Output

{'Record1': {'Mark': 100,
             'Name': 'Jack',
             'Subject': 'Math'},
 'Record2': {'Mark': 100,
             'Name': 'Rack',
             'Subject:': 'Math'},
 'Record3': {'Mark': 100,
             'Name': 'Max',
             'Subject': 'Music'},
 'Record4': {'Mark': 100,
             'Name': 'David',
             'Subject': 'Math'}}

4. Join() and for loop to Print nested dictionary line by line in Python


In this, We have used join() along with generator expression to print the dictionary line by line.

data_dict ={'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'},
'Record4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}

print('\n'.join("%s\n%s" % (key1,('\n'.join("%s : %r" % (key2,val2) for (key2,val2) in val1.items()))) for (key1,val1) in data_dict.items()))



Output

Record1
Name : 'Jack'
Mark : 100
Subject : 'Math'
Record2
Name : 'Rack'
Mark : 100
Subject: : 'Math'
Record3
Name : 'Max'
Mark : 100
Subject : 'Music'
Record4
Name : 'David'
Mark : 100
Subject : 'Math'

5. Json.dump() to Print nested dictionary line by line in Python


In this Python program, we will understand how to print dictionaries in python using json.dump() method.We have used the json module dump() method. To install this module we use the command “pip install jsonlib” and import it using “import json” in our code.

import json

data_dict ={'Record1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'Record2':{ 'Name': 'Rack', 'Mark': 100,'Subject:':'Math'},
'Record3':{ 'Name': 'Max','Mark': 100, 'Subject': 'Music'},
'Record4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}

print(json.dumps(data_dict , indent=3))

Output

{
   "Record1": {
      "Name": "Jack",
      "Mark": 100,
      "Subject": "Math"
   },
   "Record2": {
      "Name": "Rack",
      "Mark": 100,
      "Subject:": "Math"
   },
   "Record3": {
      "Name": "Max",
      "Mark": 100,
      "Subject": "Music"
   },
   "Record4": {
      "Name": "David",
      "Mark": 100,
      "Subject": "Math"
   }
}

Summary

In this post, we have learned how to Print nested dictionaries line by line in Python by using 5 different ways.

Post navigation
← Previous Post
Next Post →

Related Posts

How to convert list of tuples to python dictionary

Append value to an existing key in a Python dictionary

Check multiple keys exist in Python Dictionary

5 Ways to check key exists in Python dictionary

Extract Multiple keys value from a Python dictionary

Ways to get Python dictionary keys as a list

Ways to get keys by value from a Python Dictionary

8 ways to Iterate a Python dictionary

Find all keys with maximum value in python dictionary

5 ways to Remove duplicates list of Python dictionaries

Recently Posted

  • Fix ValueError: could not convert string to float
  • Fixed Typeerror: nonetype object is not iterable Python
  • ValueError:Cannot convert non-finite values (NA or inf) to integer
  • TypeError:NoneType object is not subscriptable
  • How to navigate back in selenium python

Copyright © 2022 DevEnum.com