How do I print the key-value pairs of a dictionary in python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The standard way to print key-value pairs from a Python dictionary is to iterate with .items(), which returns each key-value pair as a tuple. You can loop through them with a for loop, format them with f-strings, or use pprint for nested dictionaries. For quick debugging, print(dict) shows the entire dictionary at once.
Basic: print(dict)
This prints the dictionary in its default representation. Fine for quick debugging, but not formatted for readability.
Iterating with .items()
.items() returns a view of (key, value) tuples. Unpacking into key, value in the loop gives clean access to both.
Formatting Options
!r uses repr() which adds quotes around strings, making the output copy-pasteable.
Using pprint (Pretty Print)
pprint automatically indents nested structures. It sorts keys alphabetically by default.
Using json.dumps for Pretty Output
json.dumps with indent produces clean, readable output. Note: it converts Python True to JSON true and does not handle non-serializable types.
Printing Only Keys or Values
One-Line Approaches
Printing Nested Dictionaries
Tabular Output with tabulate
Install with pip install tabulate.
Common Pitfalls
- Modifying dict during iteration: Adding or removing keys while iterating with
.items()raisesRuntimeError. Create a copy first:for k, v in list(d.items()):. - Printing large dicts:
print(big_dict)outputs everything on one line. Usepprintorjson.dumps(indent=2)for readability. - Non-string keys with json.dumps:
json.dumps({1: "a"})raisesTypeError. JSON only supports string keys. Usepprintinstead, or convert keys:json.dumps({str(k): v for k, v in d.items()}). - Order assumptions: Python 3.7+ dicts maintain insertion order. Printing shows keys in insertion order.
pprintsorts keys alphabetically. Usesort_dicts=False(Python 3.8+) to preserve order. - repr vs str:
print(value)usesstr()which may hide type information. Useprint(repr(value))orf"{value!r}"to see exact types and escape characters.
Summary
- Use
for key, value in dict.items()with f-strings for basic printing - Use
pprint.pprint()for readable nested dictionary output - Use
json.dumps(dict, indent=2)for JSON-formatted output - Use
f"{key:<10} {value}"for aligned columnar output - Use
.keys(),.values(), or.items()to access specific parts of the dictionary - For production logging, convert to JSON or use structured logging libraries
Related reading
- How do I read CSV data into a record array in NumPy?
- How do I remove duplicates from a list, while preserving order?
- How do I remove duplicates from a list, while preserving order?
- How do I remove local untracked files from the current Git working tree?
- How do I print to console in pytest?
- How do I print to stderr in Python?
- How do I remove repeated elements from ArrayList?
- How do I remove the first item from a list?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.