Write a Python program to print a dictionary line by line


The program is written in Python and it uses a dictionary data structure called "sub" to store the marks of two students, "Sam" and "Suresh", in three subjects "M1", "M2", and "M3".

The program uses two nested for loops to print the marks of each student. The outer loop iterates over the keys of the dictionary "sub", which are the names of the students, and the inner loop iterates over the values of each student, which are the marks in each subject.

The first for loop iterates over the keys of the dictionary "sub". In each iteration, the variable "a" is assigned the value of the current key, which is the name of a student.

The second for loop iterates over the values of the current key in the dictionary "sub". In each iteration, the variable "b" is assigned the value of the current key, which is the name of a subject. The marks of the student in that subject are then printed using the expression sub[a][b].

Source Code

sub = {"Sam":{"M1":89,"M2":56,"M3":89},
        "Suresh":{"M1":49,"M2":96,"M3":89}}
for a in sub:
    print(a)
    for b in sub[a]:
        print (b,":",sub[a][b])

Output

Sam
M1 : 89
M2 : 56
M3 : 89
Suresh
M1 : 49
M2 : 96
M3 : 89