Printing Lists as Tabular Data
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Displaying list data in aligned columns makes it far easier to read than raw print output. Python offers several approaches — from built-in string formatting to dedicated libraries like tabulate and pandas. The right choice depends on whether you need quick console output, export to other formats, or full data analysis capabilities.
Method 1: String Formatting with f-strings
The simplest approach uses Python's string formatting to align columns:
Format specifiers: < left-aligns, > right-aligns, ^ centers. The number sets the field width.
Method 2: The tabulate Library
tabulate is the go-to library for console tables with minimal code:
Available formats include grid, pipe (Markdown), html, latex, plain, simple, and fancy_grid:
Method 3: Using pandas DataFrame
For data analysis workflows, pandas provides built-in table display:
Method 4: str.format with Dynamic Widths
Calculate column widths automatically from the data:
Method 5: PrettyTable
Another dedicated library with an interactive feel:
Method 6: CSV Module for File Output
When you need to write tabular data to a file:
Common Pitfalls
- Mixed types break alignment: Numbers and strings have different default alignments. Convert everything to strings with
str()before formatting, or usetabulatewhich handles this automatically. - Unicode characters break column widths: Characters like CJK ideographs are double-width in terminals.
len("日本")returns 2 but displays as 4 characters wide. Useunicodedata.east_asian_width()orwcwidthlibrary for accurate widths. - Large datasets flood the console: For tables with hundreds of rows, use
df.head()/df.tail()with pandas, or pipe output through a pager likeless. - Forgetting
newline=""in CSV: On Windows, omittingnewline=""inopen()causes double line breaks in CSV files. - Hardcoded column widths: Hardcoding widths like
f"{name:20}"breaks when data exceeds the width. Calculate widths dynamically from the data.
Summary
- Use f-strings or
str.format()for quick, dependency-free console output - Use
tabulatefor formatted tables with multiple output formats (grid, Markdown, HTML, LaTeX) - Use
pandas.DataFramewhen you are already doing data analysis - Use
PrettyTablefor interactive table building with sorting and alignment - Calculate column widths dynamically from your data to handle variable-length content
- For file output, use the
csvmodule ordf.to_csv()
Related reading
- Probability distribution in Python
- Problems obtaining most informative features with scikit learn?
- Produce balanced mini batch with Dataset API
- Programmatically determine the relative popularities of a list of items books, songs, movies, etc
- Priority queue in .Net
- Priority Queue in swift
- Printing optional variable
- Printing Python version in output

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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.