Skip to content

Feature Request: Add compact table format option to reduce whitespace padding #392

@ceberam

Description

@ceberam

Description

Problem

The current table formats (especially github and pipe) add significant whitespace padding to align all columns, which creates unnecessarily large output for tables with many columns or varying cell widths. This becomes problematic when:

  1. Working with large tables (e.g., 100+ columns or 1000+ rows)
  2. Processing tables for downstream tasks like tokenization or LLM input
  3. Storing or transmitting table data where size matters

Example

Current behavior with tablefmt="github":

from tabulate import tabulate

data = [
    ["Syntax", "Description", "Test Text"],
    ["Header", "Title", "Here's this"],
    ["Paragraph", "Text", "And more"]
]

print(tabulate(data[1:], headers=data[0], tablefmt="github"))

Output (215 characters):

| Syntax      | Description   | Test Text     |
|-------------|---------------|---------------|
| Header      | Title         | Here's this   |
| Paragraph   | Text          | And more      |

Proposed Solution

Add a compact parameter (or a new compact_github/compact_pipe format) that produces minimal whitespace while maintaining valid markdown:

print(tabulate(data[1:], headers=data[0], tablefmt="github", compact=True))
# or
print(tabulate(data[1:], headers=data[0], tablefmt="compact_github"))

Desired output (145 characters, 32% smaller):

| Syntax | Description | Test Text |
| - | - | - |
| Header | Title | Here's this |
| Paragraph | Text | And more |

Key Requirements

  1. Remove column padding: No extra spaces to align columns
  2. Minimal separators: Use single dash (-) instead of multiple dashes (-------)
  3. Preserve alignment marks: Keep :---, :---:, and ---: for left/center/right alignment
  4. Maintain validity: Output should still be valid markdown

Example with Alignment

Input with alignment:

data = [
    ["Left", "Center", "Right"],
    ["A", "B", "C"]
]
# Assuming alignment can be specified somehow

Compact output should preserve alignment marks:

| Left | Center | Right |
| :- | :-: | -: |
| A | B | C |

Benefits

  • Space savings: 30-60% reduction in character count for typical tables
  • Performance: Faster processing for large tables
  • Compatibility: Still produces valid markdown tables
  • Use cases: Better for LLM tokenization, data transmission, storage

Additional Context

Environment:

  • Python version: 3.12+
  • tabulate version: 0.9.0

I'm happy to contribute a PR if there's interest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions