Print a Dictionary in Table format in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Print a dictionary in table format in Python
  2. Printing a dictionary without list values in a Table format
  3. Print a dictionary in table format with keys as headers
  4. Print a dictionary in table format using pandas

# Print a dictionary in table format in Python

To print a dictionary in table format:

  1. Use a formatted string literal to print the headers.
  2. Iterate over the items of the dictionary and format each row.
  3. Use the print() function to print the result.
main.py
my_dict = { 1: ['alice', 29], 2: ['bobbyhadz', 30], 3: ['carl', 31], } headers = ['ID', 'Name', 'Age'] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Age # 1 alice 29 # 2 bobbyhadz 30 # 3 carl 31 for key, value in my_dict.items(): print(f'{key: <10}{value[0]: <15}{value[1]}')

print dictionary in table format

The code for this article is available on GitHub

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 dictionary and print each row.

# Printing a dictionary without list values in a Table format

You can use the same approach to print a dictionary in table format if the dictionary doesn't have list values.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } # id 1 # name bobbyhadz # age 30 for key, value in my_dict.items(): print(f'{key: <10}{value}')

printing dictionary without list values in table format

The code for this article is available on GitHub

You can use the same approach if you need to use the dictionary's keys as headers in the table.

# Print a dictionary in table format with keys as headers

This is a three-step process:

  1. Use the dict.keys() method to get a view of the dictionary's keys and format them.
  2. Iterate over the items of the dictionary and format each row.
  3. Use the print() function to print the result.
main.py
my_dict = { 'id': [1, 'alice', 29], 'name': [2, 'bobbyhadz', 30], 'age': [3, 'carl', 31], } headers = list(my_dict.keys()) print( f'{headers[0].capitalize(): <10}{headers[1].capitalize(): <15}{headers[2].capitalize()}') # Id Name Age # 1 alice 29 # 2 bobbyhadz 30 # 3 carl 31 for _key, value in my_dict.items(): print(f'{value[0]: <10}{value[1]: <15}{value[2]}')
The code for this article is available on GitHub

We used the dict.keys() method to get a view of the dictionary's keys and used an f-string to format the headers.

The dict.keys() method returns a new view of the dictionary's keys.

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.keys()) # ๐Ÿ‘‰๏ธ dict_keys(['id', 'name'])

We also used the str.capitalize() method to capitalize the first letter in each key, but this is optional.

The str.capitalize() function returns a copy of the string with the first character capitalized and the rest lowercased.

main.py
print('bobby'.capitalize()) # ๐Ÿ‘‰๏ธ 'Bobby' print('HADZ'.capitalize()) # ๐Ÿ‘‰๏ธ 'Hadz'

You can also use the str.upper() method if you need to convert each key to uppercase.

# Print a dictionary in table format using pandas

You can also use the pandas module to print a dictionary in table format.

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

shell
pip install pandas # ๐Ÿ‘‡๏ธ or with pip3 pip3 install pandas

Now you can import and use the pandas module to create a DataFrame from the dictionary.

main.py
import pandas as pd my_dict = { 'Name': ['Alice', 'Bobby', 'Carl'], 'Age': [29, 30, 31] } df = pd.DataFrame(data=my_dict) # Name Age # 0 Alice 29 # 1 Bobby 30 # 2 Carl 31 print(df)

print dictionary in table format using pandas

The code for this article is available on GitHub

The DataFrame class is used to create two-dimensional tabular data.

Here is another example.

main.py
import pandas as pd my_dict = { 1: ['alice', 29], 2: ['bobbyhadz', 30], 3: ['carl', 31], } df = pd.DataFrame( [[key] + list(value) for key, value in my_dict.items()], columns=['ID', 'Name', 'Age'], ).set_index('ID') df = df.reset_index(drop=True) # Name Age # 0 Alice 29 # 1 Bobby 30 # 2 Carl 31 print(df)

The code sample explicitly sets the columns of the table.

I've also written an article on how to print specific key-value pairs in a dictionary.

# 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.

Copyright ยฉ 2024 Borislav Hadzhiev