In this python dictionary program, we will print a dictionary line by line.
Steps to solve the program
- Take a dictionary as input.
- Use for loop to iterate over keys and values of the dictionary using items().
- First, print the key.
- Using nested for loop iterate over keys and values in the value of the respective key.
- Print the key-value pair.
dict1 = {'virat':{'sport':'cricket','team':'india'},
'messi':{'sport':'football','team':'argentina'}}
for key, val in dict1.items():
print(key)
for k,v in val.items():
print(k,":",v)
Output :
virat
sport : cricket
team : india
messi
sport : football
team : argentina