Print a list in Tabular format in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Print a list in Tabular format in Python
  2. Print a list in Tabular format using tabulate
  3. Print a list in Tabular format using prettytable

Print a list in Tabular format in Python #

To print a list in tabular format:

  1. Use a formatted string literal to print the headers.
  2. Iterate over the list and format each row.
  3. Use the print() function to print the result.
main.py
headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
The example shows how to print a list of lists in tabular format, if you have a one-dimensional list, scroll down to the next example.

We used a formatted string literal to format the headers and rows.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'

The space between the colon and the less-than sign is the fill character.

The less-than or greater-than sign is the alignment.

The less-than sign aligns the string to the left and the greater-than sign aligns the string to the right.

We first format and print the headers and then iterate over the list and print each row.

You can use the zip() function if you need to print a one-dimensional list in a table.

main.py
headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for first, second, third in zip(my_list[::columns], my_list[1::columns], my_list[2::columns]): print(f'{first: <10}{second: <15}{third}')

The zip function iterates over several iterables in parallel and produces tuples with an item from each iterable.

The zip function returns an iterator of tuples.

main.py
headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 # [(1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada')] print(list(zip(my_list[::columns], my_list[1::columns], my_list[2::columns])))

We used list slicing in the call to the zip() function with every 3 elements in the list, starting at index 0, then at index 1, etc.

Alternatively, you can use the tabulate package.

Print a list in Tabular format using tabulate #

To print a list in tabular format:

  1. Run the pip install tabulate command to install the tabulate module.
  2. Pass the list as an argument to the tabulate() method.

First, open your terminal in your project's root directory and install the tabulate module.

shell
pip install tabulate

Now we can import and use the tabulate method.

main.py
from tabulate import tabulate headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # ID Name Country # ---- --------- --------- # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada print(tabulate(employees, headers=headers))

As the documentation states, the following tabular data types are supported:

  • list of lists or another iterable of iterables
  • list or another iterable of dictionaries (keys as columns)
  • dictionary of iterables (keys as columns)
  • two-dimensional Numpy arrays
  • NumPy record arrays (names as columns)
  • pandas.DataFrame

The tabulate() method also takes a tablefmt argument that can be used to format the table.

main.py
from tabulate import tabulate headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # | ID | Name | Country | # |------|-----------|-----------| # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | print(tabulate(employees, headers=headers, tablefmt='github'))

If you need to test some of the other options, scroll down to the "Table format" section in the documentation.

You can also set the headers argument to "firstrow" if your headers are stored in the first row of the list.

Alternatively, you can use the prettytable module.

Print a list in Tabular format using prettytable #

To print a list in tabular format:

  1. Run the pip install prettytable command to install the prettytable module.
  2. Use the PrettyTable class from the module to print the list in tabular format.

First, open your terminal and install the prettytable module.

shell
python -m pip install -U prettytable

Now we can import and use the PrettyTable class.

main.py
from prettytable import PrettyTable headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] pt = PrettyTable(headers) pt.add_rows(employees) # +----+-----------+----------+ # | ID | Name | Country | # +----+-----------+----------+ # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | # +----+-----------+----------+ print(pt)

The prettytable module can also be used for sorting.

You can also change the formatting of the table.

main.py
from prettytable import PrettyTable, MSWORD_FRIENDLY headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] pt = PrettyTable(headers) pt.set_style(MSWORD_FRIENDLY) pt.add_rows(employees) # | ID | Name | Country | # | 1 | alice | Austria | # | 2 | bobbyhadz | Bulgaria | # | 3 | carl | Canada | print(pt)

You can view some of the other formatting options in the "Setting a table style" section of the documentation.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.