2025, Sep 24 09:00

User-friendly printing of list-of-tuples in Python: iterate, unpack, format, and enumerate

Learn how to print a Python list of tuples cleanly by iterating, tuple-unpacking, and using f-strings; add indexes with enumerate for clear, compact output.

User-friendly printing of list-of-tuples in Python

Working with a list of tuples like (subject, mark) is common, but dumping the raw list to stdout isn’t great for readability. If you simply print the list, you get tuple syntax instead of a clean, human-friendly report. Let’s walk through the minimal change that makes the output clear without altering the underlying data structure or logic.

Problem example

The data is stored as a list of (subject, mark) pairs. The current print function outputs the list object directly, which results in Python’s default representation of tuples.

entries = [('English', 78), ('Mathematics', 99)]

def show_report():
    print(entries)

show_report()

This prints something like:

[('English', 78), ('Mathematics', 99)]

That’s technically correct but not user-friendly.

What went wrong

Attempting to iterate with range over a list or a string leads to a TypeError because range expects an integer, not a list. The idiomatic approach in Python is to iterate directly over the iterable. Since each item in the list is a tuple, it’s convenient to unpack the tuple into two variables during iteration.

The general form is straightforward: for item in iterable:. Applying this to a list of tuples lets you unpack values on the fly.

The fix: iterate, unpack, format

Iterate over the list, unpack each (subject, mark) pair, and format with an f-string for clarity. This keeps the data model and overall behavior intact, while improving display.

entries = [('English', 78), ('Mathematics', 99)]

def show_report():
    for topic, score in entries:
        print(f"{topic} => {score}")

show_report()

The output becomes concise and readable:

English => 78
Mathematics => 99

Optional indexing with enumerate

If you want line numbers, enumerate assigns an index to each item during iteration. This is useful when you need positional context in the output. The unpacking works the same way, and you can start counting from 1.

entries = [('English', 78), ('Mathematics', 99)]

def show_report_numbered():
    for idx, (topic, score) in enumerate(entries, start=1):
        print(f"{idx}. For subject {topic} the mark is {score}")

show_report_numbered()

The output then includes numbering:

1. For subject English the mark is 78
2. For subject Mathematics the mark is 99

Why this matters

Readable output reduces friction in everyday tasks such as quick validation, demos, and console-based workflows. Direct iteration over an iterable and tuple unpacking are basic tools that prevent common errors, keep code compact, and make results easier to scan.

Conclusion

When you store related values in tuples, iterate over the list and unpack the pair right in the loop. Combine that with an f-string to format the output cleanly. If you need positional information, enumerate is a simple extension that adds indexes without changing your data or control flow.

The article is based on a question from StackOverflow by PCuser2009 and an answer by anatolyg.