How to Nest Dictionaries in Python


In this example we will show how to nest many dictionaries that already exist as dictionaries in Python.

Source Code

student1 = {
    'name' : 'Jim',
    'id' : 12
}
student2 = {
    'name' : 'Steven',
    'id' : 15
},
student3 = {
    'name' : 'Lucy',
    'id' : 18
}

student = {
    'student1' : student1,
    'student2' : student2,
    'student3' : student3,
}

print(student)

Output:

{'student1': {'name': 'Jim', 'id': 12}, 'student2': ({'name': 'Steven', 'id': 15},), 'student3': {'name': 'Lucy', 'id': 18}}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments