AI Skill Report Card

Pretty Printing Python Data

B68·Aug 22, 2026·Source: Web
13 / 15
Python
from 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
10 / 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_dicts as 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 within width
  • sort_dicts=False — preserve insertion order instead of alphabetical sorting (default True sorts 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 (for pprint())

Other Useful Functions

  • pprint.pp(obj, *args, sort_dicts=False, **kwargs) — shorthand for pprint() with sort_dicts=False by default (better for preserving dict order)
  • pprint.isreadable(obj) — checks if the formatted repr can be eval()'d back
  • pprint.isrecursive(obj) — checks if the object contains a recursive reference
  • pprint.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
14 / 20

Example 1: Deeply nested structure with default settings

Input:

Python
from 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:

Python
pprint(data, depth=2)

Output:

{'a': {'b': {...}}}

Example 3: Preserving insertion order (avoiding alphabetical resort)

Input:

Python
pprint({"z": 1, "a": 2, "m": 3}, sort_dicts=False)

Output:

{'z': 1, 'a': 2, 'm': 3}

Example 4: Using pformat for logging

Input:

Python
import 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:

Python
pprint([(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
  • Default sort_dicts=True reorders keys alphabetically — use sort_dicts=False (or pprint.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; reserve pprint() for interactive/debug console output.
  • For custom objects, implement __repr__ well — pprint relies on repr for leaf values.
  • Increase width for structures with long strings to avoid excessive wrapping; decrease it for narrow terminals.
  • Assuming pprint preserves dict insertion order by default — it does not (sorts keys unless sort_dicts=False).
  • Using pprint on 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 — calling print(pprint(x)) prints None because pprint() already prints and returns None.
  • Expecting pprint to "fix" unreadable custom objects — it only pretty-formats structure, not the repr of leaf objects.
0
Grade BAI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
10/15
Examples
14/20
Completeness
14/20
Format
14/15
Conciseness
13/15