Python: Print a dictionary line by line
Python dictionary: Exercise-32 with Solution
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
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-32.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics