How to display a List as a Table in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. How to display a List as a Table in Jupyter Notebook
  2. Display Lists as Tables in Jupyter Notebook using tabulate
  3. Display lists as tables by manually building the HTML

# How to display a List as a Table in Jupyter Notebook

You can use a DataFrame to display lists as tables in Jupyter Notebook.

Import and instantiate the DataFrame class, passing it the list and the names of the columns.

main.py
import pandas as pd a_list = [ [1, 2, 3], [4, 5, 6] ] pd.DataFrame(a_list, columns=["First", "Second", "Third"])

display lists as tables

Make sure to specify exactly as many column names as there are columns in the list.

The list from the example has 2 rows and 3 columns, so I passed 3 column names to the columns array when instantiating the DataFrame class.

If your list has many columns, they might not all be displayed.

You can set the max_columns attribute to None to display all columns.

main.py
import pandas as pd a_list = [ [1, 2, 3], [4, 5, 6] ] # 👇️ set max_columns to None pd.options.display.max_columns = None pd.DataFrame(a_list, columns=["First", "Second", "Third"])

You can also use the IPython HTML and display functions to print the table as HTML.

main.py
from IPython.display import HTML, display import pandas as pd a_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], ] df = pd.DataFrame(a_list, columns=['First', 'Second', 'Third']) display(HTML(df.to_html()))

print dataframe as table using html and display

Make sure you have pandas installed by running the following command in a cell.

shell
!pip install pandas

Or one of the following commands from your terminal.

shell
pip install pandas pip3 install pandas

# Display Lists as Tables in Jupyter Notebook using tabulate

You can also use the tabulate module to display lists as tables.

Make sure you have the module installed by running the following command in a cell.

shell
!pip install tabulate

Or one of the following commands from your terminal.

shell
pip install tabulate pip3 install tabulate

Now, import and use the module as follows.

main.py
from tabulate import tabulate from IPython.display import HTML, display a_list = [ [1, 2, 3], [4, 5, 6] ] table = tabulate(a_list, tablefmt='html') display(HTML(table))

display list in table format using tabulate

The code sample also uses the HTML() and display() functions from IPython's display module.

The tabulate module is used to pretty-print tabular data.

Here is an example from the docs that better illustrates how it works.

main.py
from tabulate import tabulate from IPython.display import HTML, display a_list = [ ["Sun", 696000, 1989100000],["Earth",6371 ,5973.6], ["Moon",1737, 73.5],["Mars", 3390, 641.85] ] table = tabulate(a_list, tablefmt='html') display(HTML(table))

tabulate print list as table

If you want to use the first row as the headers of the table, set the headers keyword argument to firstrow.

main.py
from tabulate import tabulate from IPython.display import HTML, display a_list = [ ["Name", "Age", "Salary"], ["Alice", 29, 60], ["Bobby", 30, 40], ["Carl", 31, 80] ] table = tabulate( a_list, tablefmt='html', headers='firstrow' ) display(HTML(table))

use headers as first row

You can view more examples of how to use the tabulate module in the package's pypi page.

The display and HTML functions are needed to display the HTML we get from calling tabulate() in a table format.

Alternatively, you can manually build the HTML and pass it to the functions.

# Display lists as tables by manually building the HTML

Here is an example that manually builds the HTML for the table based on the list.

main.py
from IPython.display import HTML, display a_list = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], ] def list_to_table(a_list): html = '<table>' for row in a_list: html += '<tr>' for element in row: html += f'<td><h4>{element}</h4></td>' html += '</tr>' html += '</table>' display(HTML(html)) list_to_table(a_list)

manually build html for table

The code sample constructs an HTML table by iterating over the list.

The outer for loop iterates over the rows in the two-dimensional list and the inner loop iterates over

Once the HTML is constructed, we pass it to the HTML() and display() functions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.