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.