Skip to content

Write a Python program to print a dictionary line by line | Example code

  • by

Using two for loops with logic you can write a Python program to print a dictionary line by line. Iterate the dictionary with the key and value at the same time and print it.

for key, value in student_score.items():
    print(key, ' : ', value)

Python program to print a dictionary line by line

Simple example code.

cars = {'A': {'speed': 70,
              'color': 2},
        'B': {'speed': 60,
              'color': 3}}

for x in cars:
    print(x)
    for y in cars[x]:
        print(y, ':', cars[x][y])

Output:

Write a Python program to print a dictionary line by line

Source: stackoverflow.com

Do comment if you have any doubts or suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.