When your data grows beyond a toy example,
pprint
starts to break.
You see cut-off arrays. Flattened hierarchies. Lost context.
And worst of all? Hidden bugs buried under “pretty” formatting.
That’s why I created SetPrint — a Python library that shows structure, not just values.
🔍 What this article covers
- ✅ Side-by-side comparisons:
pprint
/setprint
- ✅ Real-world examples: image data, confusion matrices
- ✅ Benchmarks + 5 must-know tips for structured debugging
🧪 Quick Colab Demo
Want to see it in action? Try this demo notebook — no install needed.
(To use Colab, a Google account is required.)
🔗 Try this notebook on Google Colab
1. Visual Comparison — pprint vs setprint
data = {
"users": [
{"name": "Alice", "scores": np.array([95, 88, 76])},
{"name": "Bob", "scores": np.array([72, 85, 90])}
],
"meta": {"created": "2025-04-23", "version": 1.2}
}
# pprint
from pprint import pprint
pprint(data)
# setprint
from pprint import pprint
for line in SetPrint(data).set_collection(
route='SLIM', y_axis=False, keep_settings={1: 'y'}
):
print(line)
== pprint ==
{'meta': {'created': '2025-04-23', 'version': 1.2},
'users': [{'name': 'Alice', 'scores': array([95, 88, 76])},
{'name': 'Bob', 'scores': array([72, 85, 90])}]}
== setprint ==
In this example, each depth level is expanded vertically (`'y'`),
which helps preserve the nested structure clearly:
keep_settings = { 1:'y', 2:'y', 3:'y', 4:'y'}
# deep1:'y' ~ deep4:'y'
--------------------------------------------------------
y y y y
◆dict
├── users:►list
│ ├───── -------. ◆dict
│ │ ├───────── name : Alice
│ │ └───────── scores:>ndarray
│ │ ├─────── 95
│ │ ├─────── 88
│ │ └─────── 76
│ └───── -------. ◆dict
│ ├───────── name : Bob
│ └───────── scores:>ndarray
│ ├─────── 72
│ ├─────── 85
│ └─────── 90
└── meta :◆dict
├───── created:2025-04-23
└───── version: 1.2
--------------------------------------------------------
Even at a glance, the structure is crystal clear.
2. Confusion Matrices as Text-Based Heatmaps
sample = [[0, 1, 2, 3], [0, 1, 2, 3], [0, ['bug'], 2, 3], [0, ['bug','bug'], 2, 3]]
# pprint
from pprint import PrettyPrinter
pp = PrettyPrinter(width=30)
pp.pprint(sample)
# setprint
from setprint import SetPrint
for line in SetPrint(sample).set_collection(
route='SLIM', y_axis=False,
keep_settings={1: 'y', 2: 'x'}
):
print(line)
== pprint ==
[[0, 1, 2, 3],
[0, 1, 2, 3],
[0, ['bug'], 2, 3],
[0, ['bug', 'bug'], 2, 3]]
== setprint ==
keep_settings
['y', 'x', 'x']
--------------------------------
►list
├── ►list ┬───┬───────────┬─┐
│ 0 1 2 3
├── ►list ┬───┬───────────┬─┐
│ 0 1 2 3
├── ►list ┬───┬───────────┬─┐
│ 0 ►list ─┐ 2 3
│ bug
└── ►list ┬───┬───────────┬─┐
0 ►list ─┬───┐ 2 3
bug bug
--------------------------------
3. Image Arrays? No Problem
rgb = np.random.randint(0, 255, size=(3, 3, 3))
set_collection ( route='SLIM', y_axis=False, keep_settings={1: 'y', 2: 'x',3: 'x'} )
# pprint
from pprint import pprint
pprint(rgb)
# setprint
from setprint import SetPrint
for line in SetPrint(rgb).set_collection(
route='SLIM', y_axis=False,
keep_settings={1: 'y', 2: 'x'}
):
print(line)
===== print =====
[[[255 0 4]
[255 85 0]
[255 170 0]]
[[170 255 0]
[ 85 255 0]
[ 0 255 4]]
[[ 0 170 255]
[ 0 85 255]
[ 4 0 255]]]
== setprint ==
keep_settings
['y', 'x', 'x']
---------------------------------------------------------------------------------
>ndarray
├──── >ndarray ───┬────────────────────┬────────────────────┐
│ >ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐
│ 255 0 4 255 85 0 255 170 0
├──── >ndarray ───┬────────────────────┬────────────────────┐
│ >ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐
│ 170 255 0 85 255 0 0 255 4
└──── >ndarray ───┬────────────────────┬────────────────────┐
>ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐ >ndarray ─┬───┬───┐
0 170 255 0 85 255 4 0 255
---------------------------------------------------------------------------------
You don’t just see the values—you see the hierarchy.
4. Benchmark: Visibility vs Speed
Library | Runtime (ms) | Structure Clarity |
---|---|---|
pprint | 1.5 | ★☆☆☆☆ |
rich.pretty | 3.2 | ★★☆☆☆ |
setprint | 4.8 | ★★★★★ |
A few ms extra for full visibility? Totally worth it.
5. 5 Must-Know Tips for Using SetPrint
- You can customize how each depth level is displayed using a
{depth: direction}
mapping.
This helps you clearly map the structure of complex data like nested lists, matrices, or even image arrays.
- Show vertical guides with
y_axis=True
- Output is a list of lines → easy to write to file
- Handles mixed types:
dict
+list
+ndarray
? No problem - Switch between styles like
'SLIM'
,'BOLD'
, or even'HALF'
6. One Function, Infinite Views
from setprint import SetPrint
list_data = SetPrint(data)
formatted = list_data.set_collection(
keep_settings={1:'x', 2:'yf', 3:'f'},
route='SLIM', y_axis=True
)
for line in formatted:
print(line)
🚀 Try it Now
pip install setprint
🎯 If you found it useful, please consider giving a ⭐ on GitHub!
🐛 Bug reports, 💡 feature requests, and 📬 pull requests are all welcome!
📎 GitHub: mtur2007/SetPrint
📘 PyPI: setprint
🔍 Colab Demo: Try on Colab
🔗 Also available on Medium!
💬 I'd love to hear your feedback on either platform. ✨
Still polishing things up, but if you know anyone frustrated with pprint,
I’d really appreciate it if you could just spread the word.
(Thank you so much for reading to the end.
As the creator of this library, I’m truly grateful.)
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.