Print a dictionary in table format in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

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]}')

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.

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}')

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 #

To print a dictionary in table format:

  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]}')

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.

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.