How To Print A List In Tabular Format In Python

To print a list in Tabular format in Python, you can use the format(), PrettyTable.add_rows(), tabulate() functions, or pandas.DataFrame() object. See details below.

Print A List In Tabular Format In Python

Using the format() function

You can use the format() function to print a list in Tabular format in Python. In this case, we will use the format() function to print each item in the initial list with the specified format. 

Look at the example below.

# Create a list.
customers = [
    ['Alex', 'New York', 23],
    ['Fred', 'Manchester', 35],
    ['Casemiro', 'Manchester', 28],
    ['Shaw', 'Madrid', 27]
]

# Print the header.
print('{:<7} {:<14} {:<9}'.format('name', 'address', 'age'))

# Print a list in Tabular format with the format() function.
for item in customers:
    name, address, age = item
    print('{:<7} {:<14} {:<9}'.format(name, address, age))

Output

name    address        age      
Alex    New York       23
Fred    Manchester     35
Casemiro Manchester     28
Shaw    Madrid         27

Using the tabulate() function

Besides the format() function, you can use the tabulate() function to print a list in Tabular format in Python. It is built into the tabulate package. So you have to install the tabulate package first to use this function. With this function, you only need to assign the data and header as the hyperparameters.

Look at the example below.

from tabulate import tabulate
# Create a list.
customers = [
    ['Alex', 'New York', 23],
    ['Fred', 'Manchester', 35],
    ['Casemiro', 'Manchester', 28],
    ['Shaw', 'Madrid', 27]
]

# Print a list in Tabular format with the tabulate() function.
tabular_table = tabulate(customers, headers=['name', 'address', 'age'])
print(tabular_table)

Output

name      address       age
--------  ----------  -----
Alex      New York       23
Fred      Manchester     35
Casemiro  Manchester     28
Shaw      Madrid         27

Using the PrettyTable.add_rows() function

Moreover, you can use the PrettyTable.add_rows() function to print a list in Tabular format. Simply, you create a PrettyTable object first. Then, you add your data to this object with the PrettyTable.add_rows() function.

Look at the example below.

from prettytable import PrettyTable
# Create a list.
customers = [
    ['Alex', 'New York', 23],
    ['Fred', 'Manchester', 35],
    ['Casemiro', 'Manchester', 28],
    ['Shaw', 'Madrid', 27]
]

# Create a PrettyTable object.
pretty_table = PrettyTable(['name', 'address', 'age'])

# Add data to this table with the add_rows() function.
pretty_table.add_rows(customers)
print(pretty_table)

Output

+----------+------------+-----+
|   name   |  address   | age |
+----------+------------+-----+
|   Alex   |  New York  |  23 |
|   Fred   | Manchester |  35 |
| Casemiro | Manchester |  28 |
|   Shaw   |   Madrid   |  27 |
+----------+------------+-----+

Using the Pandas.DataFrame() object

In addition, you can use Pandas.DataFrame() object to print a list in Tabular format. More simply than the three ways above, you only need to create a Pandas.DataFrame() object with the specified data and print it out.

Look at the example below.

from pandas import DataFrame

# Create a list.
customers = [
    ['Alex', 'New York', 23],
    ['Fred', 'Manchester', 35],
    ['Casemiro', 'Manchester', 28],
    ['Shaw', 'Madrid', 27]
]

# # Print a list in Tabular format with the pandas.DataFrame() function.
df = DataFrame(customers, columns=['name', 'address', 'age'])
print(df)

Output

       name     address  age
0      Alex    New York   23
1      Fred  Manchester   35
2  Casemiro  Manchester   28
3      Shaw      Madrid   27

Besides, you can learn how to print a dictionary in table format here.

Summary

We have shown you how to print a list in Tabular format in Python in 4 ways. Personally, you should use the tabulate() function because it is simple and the result is readable. Hope you have an enjoyable learning experience. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *