How To Print A Dictionary In Table Format In Python

To print a dictionary in table format in Python, you can use the print() method, zip() function or pandas.DataFrame() object. See details below.

Print A Dictionary In Table Format In Python

Using the print() method

You can use the print() method to print a dictionary in table format in Python. First, you print the header of the table. Then, you print all rows of the table from the initial dictionary

Look at the example below.

# Create a dictionary.
athletes = {
    1: ['Carlos', 'Soccer', '06:00'],
    2: ['Chiesa', 'Soccer', '06:30'],
    3: ['Nadal', 'Badminton', '09:00'],
    4: ['Valverde', 'Swimming', '15:00'],
    5: ['Bolt', 'Volleyball', '15:30']
}

# Print the header of the table.
header_list = ['name', 'sport', 'time']
print(f'{header_list[0]: <9}{header_list[1]: <14}{header_list[2]}')

# Print rows of the table from the initial dictionary.
for key, value in athletes.items():
    print(f'{value[0]: <9}{value[1]: <14}{value[2]}')

Output

name     sport         time
Carlos   Soccer        06:00
Chiesa   Soccer        06:30
Nadal    Badminton     09:00
Valverde Swimming      15:00
Bolt     Volleyball    15:30

Using the zip() function 

Besides the print() method, you can use the zip() function to print a dictionary in table format.

Look at the example below.

# Create a dictionary.
athletes = {
    'name': ['Carlos', 'Chiesa', 'Valverde', 'Nadal', 'Bolt'],
    'sport': ['Soccer', 'Soccer', 'Swimming', 'Badminton', 'Volleyball'],
    'time': ['06:00', '06:45', '09:30', '15:30', '16:00']
}

# Using the zip() function to print a dictionary in table format.
for row in zip(*([i] + (j) for i, j in athletes.items())):
    print(*row, " ")

Output

name sport time  
Carlos Soccer 06:00  
Chiesa Soccer 06:45  
Valverde Swimming 09:30  
Nadal Badminton 15:30  
Bolt Volleyball 16:00  

Using the pandas.DataFrame() object

Moreover, you can use the pandas.DataFrame() object to print a dictionary in table format. It is an object whose structure is a table. So, after converting, you print the DataFrame to get the desired result.

Look at the example below.

import pandas

# Create a dictionary.
athletes = {
    'name': ['Carlos', 'Chiesa', 'Valverde', 'Nadal', 'Bolt'],
    'sport': ['Soccer', 'Soccer', 'Swimming', 'Badminton', 'Volleyball'],
    'time': ['06:00', '06:45', '09:30', '15:30', '16:00']
}

# Convert the dictionary to the pandas.DataFarme() object.
df = pandas.DataFrame(athletes)
print(df)

Output

       name       sport   time
0    Carlos      Soccer  06:00
1    Chiesa      Soccer  06:45
2  Valverde    Swimming  09:30
3     Nadal   Badminton  15:30
4      Bolt  Volleyball  16:00

Summary

We have shown you how to print a dictionary in table format in Python in 3 ways. In our point of view, you should use the pandas.DataFrame() object because you will usually use it when working with the table in Python. Hope you have an enjoyable learning experience. Thanks!

Leave a Reply

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