Sometimes, we want to print lists as tabular data with Python.
In this article, we’ll look at how to print lists as tabular data with Python.
How to print lists as tabular data with Python?
To print lists as tabular data with Python, we can use the tabulate library.
We install it by running:
pip install tabulate
For instance, we write:
from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))
We import the tabulate function from the tabulate module.
Then we call tabulate with a nested list and set the headers parameter to a list of headers for each row.
Therefore, we get:
Name Age
------ -----
Alice 24
Bob 19
printed as a result.
Conclusion
To print lists as tabular data with Python, we can use the tabulate library.