AI Skill Report Card
Pretty Printing Python Data
Quick Start13 / 15
Pythonfrom pprint import pprint, pformat data = {"name": "Alice", "roles": ["admin", "editor"], "meta": {"age": 30, "active": True}} pprint(data) # {'meta': {'active': True, 'age': 30}, # 'name': 'Alice', # 'roles': ['admin', 'editor']}
For getting a string instead of printing directly, use pformat(data).
Recommendation▾
This is a fairly narrow, low-complexity topic (a single stdlib module) — the workflow checklist feels inflated for something this simple; a simpler reference format might suit better
Workflow10 / 15
Progress:
- Step 1: Identify the data structure to display (dict, list, tuple, set, dataclass, or nested combination)
- Step 2: Choose the right function —
pprint()to print directly,pformat()to get a string (e.g., for logging) - Step 3: Tune parameters based on structure size/shape
- Step 4: Verify output width/depth is readable; adjust
width,depth,indent,compact,sort_dictsas needed
Key Parameters
indent=N— spaces per nesting level (default 1)width=N— max line width before wrapping (default 80)depth=N— max nesting levels shown; deeper levels collapse to...compact=True— pack as many items per line as fit withinwidthsort_dicts=False— preserve insertion order instead of alphabetical sorting (defaultTruesorts keys!)underscore_numbers=True— insert_every 3 digits in large numbers (e.g.,1_234_567)stream=— file object to write to instead of stdout (forpprint())
Other Useful Functions
pprint.pp(obj, *args, sort_dicts=False, **kwargs)— shorthand forpprint()withsort_dicts=Falseby default (better for preserving dict order)pprint.isreadable(obj)— checks if the formatted repr can beeval()'d backpprint.isrecursive(obj)— checks if the object contains a recursive referencepprint.saferepr(obj)— repr that handles recursive structures without raising
Recommendation▾
Add an example showing dataclasses or custom objects with __repr__, since that's mentioned as a best practice but never demonstrated
Examples14 / 20
Example 1: Deeply nested structure with default settings
Input:
Pythonfrom pprint import pprint data = {"a": {"b": {"c": {"d": [1, 2, 3, {"e": "deep"}]}}}} pprint(data)
Output:
{'a': {'b': {'c': {'d': [1, 2, 3, {'e': 'deep'}]}}}}
Example 2: Limiting depth for large nested objects
Input:
Pythonpprint(data, depth=2)
Output:
{'a': {'b': {...}}}
Example 3: Preserving insertion order (avoiding alphabetical resort)
Input:
Pythonpprint({"z": 1, "a": 2, "m": 3}, sort_dicts=False)
Output:
{'z': 1, 'a': 2, 'm': 3}
Example 4: Using pformat for logging
Input:
Pythonimport logging from pprint import pformat logging.debug("Payload:\n%s", pformat(large_dict))
Output: Logs the formatted structure as a single string, no direct printing to stdout.
Example 5: Wide list of tuples, compact mode
Input:
Pythonpprint([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')], compact=True, width=40)
Output:
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
Recommendation▾
Show a 'bad outcome' example, e.g., what happens with a circular reference or when pformat's return value is misused (print(pprint(x)) printing None) — currently only described, not demonstrated with actual output
Best Practices
- Default
sort_dicts=Truereorders keys alphabetically — usesort_dicts=False(orpprint.pp) when key order matters (e.g., JSON-like configs, ordered results). - Use
depth=to truncate huge nested structures instead of dumping everything. - Use
pformat()when integrating with logging or writing to files; reservepprint()for interactive/debug console output. - For custom objects, implement
__repr__well — pprint relies on repr for leaf values. - Increase
widthfor structures with long strings to avoid excessive wrapping; decrease it for narrow terminals.
Common Pitfalls
- Assuming
pprintpreserves dict insertion order by default — it does not (sorts keys unlesssort_dicts=False). - Using
pprinton objects with circular references without knowing it handles them safely (it will show<Recursion on ... with id=...>rather than crashing — no need to pre-check, but be aware of the output format). - Forgetting
pformat()returns a string — callingprint(pprint(x))printsNonebecausepprint()already prints and returnsNone. - Expecting pprint to "fix" unreadable custom objects — it only pretty-formats structure, not the repr of leaf objects.