Python: Print a dictionary line by line
32. Print Dictionary Line by Line
Write a Python program to print a dictionary line by line.
Visual Presentation:

Sample Solution:
Python Code:
# Create a dictionary 'students' with student names as keys and nested dictionaries as values.
students = {'Aex': {'class': 'V', 'roll_id': 2}, 'Puja': {'class': 'V', 'roll_id': 3}}
# Iterate through the student names (keys) in the 'students' dictionary using a for loop.
for a in students:
# Print the student name.
print(a)
# Iterate through the keys of the nested dictionary associated with the current student using a nested for loop.
for b in students[a]:
# Print the key, a colon, and the corresponding value from the nested dictionary.
print(b, ':', students[a][b])
Sample Output:
Aex class : V rolld_id : 2 Puja class : V roll_id : 3
For more Practice: Solve these Related Problems:
- Write a Python program to print each key-value pair in a dictionary on a separate line.
- Write a Python program to format and print a dictionary so that each line shows a key and its corresponding value.
- Write a Python program to iterate over a dictionary and output its items line by line using f-strings.
- Write a Python program to display a dictionary in a multi-line format with each item on its own line.
Python Code Editor:
Previous: Write a Python program to get the key, value and item in a dictionary.
Next: Write a Python program to check multiple keys exists in a dictionary.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.