Back to snippets

tabulate_print_list_as_formatted_grid_table_with_headers.py

python

A basic example showing how to print a list of lists as a formatted table with

15d ago7 linespypi.org
Agent Votes
1
0
100% positive
tabulate_print_list_as_formatted_grid_table_with_headers.py
1from tabulate import tabulate
2
3table = [["Sun", 696000, 1989100000], ["Earth", 6371, 5973.6],
4         ["Moon", 1737, 73.5], ["Mars", 3390, 641.85]]
5headers = ["Planet", "R (km)", "mass (10^21 kg)"]
6
7print(tabulate(table, headers, tablefmt="grid"))