How to print a dictionary line by line in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Printing a dictionary line by line in Python is usually just a matter of iterating over its items and formatting the output clearly. The right technique depends on whether the dictionary is flat or nested, whether order matters, and whether the output is for humans or for another tool.
The Basic Pattern
For a simple dictionary, iterate over items() and print one key-value pair per line.
Output:
This is the normal answer for most scripts.
Why items() Is Better Than Iterating Keys Alone
You could loop through the keys and index back into the dictionary, but items() is cleaner and avoids an extra lookup.
Less direct:
Better:
The second form makes the intent explicit and scales better when you want to transform both parts of the pair.
Printing in a Stable Order
Modern Python preserves insertion order, but sometimes you want alphabetical output instead. In that case, sort the keys or items explicitly.
If predictable ordering matters in logs, tests, or documentation, make the ordering explicit rather than depending on how the dictionary happened to be created.
Formatting for Readability
Simple print is often enough, but aligned output is easier to scan when keys vary in length.
That produces a cleaner column-like layout. It is a small improvement, but it matters when dictionaries are printed in debugging tools or CLI output.
Nested Dictionaries Need a Different Strategy
If values can themselves be dictionaries, a flat one-line print may no longer be readable. In that case, use recursion or a formatting tool.
Example recursive printer:
This is far more readable than dumping nested dictionaries as raw Python literals line by line.
Use pprint or JSON for Structured Output
If the goal is debugging rather than custom formatting, the standard library already provides good tools.
With pprint:
With JSON-style pretty printing:
Use these when you want structure preserved without writing your own formatting logic.
Converting Values for Display
Be careful when dictionary values are objects, lists, or dates. The default string representation may be correct for debugging but poor for user-facing output.
For example:
Small display rules like this can make logs and CLI output much more usable.
Common Pitfalls
The most common mistake is printing the whole dictionary object and calling that "line by line." print(data) gives one string representation, not a formatted per-entry output.
Another mistake is using for key in data when items() would make the code clearer. Both work, but items() is the right default when you need keys and values.
Developers also forget about ordering. If output order matters for testing or comparison, sort explicitly.
Finally, nested dictionaries often need special formatting. A simple one-line loop is fine for flat data, but it becomes noisy and hard to read once the values contain more structure.
Summary
- For flat dictionaries, iterate over
data.items()and print one pair per line. - Use explicit sorting when output order matters.
- Format columns if the output is meant for people to scan quickly.
- For nested dictionaries, use recursion,
pprint, or JSON pretty printing. - Treat display formatting as a separate concern when values are complex objects.

