Printing Dictionary in Python

A dictionary is a data structure that stores key-value pairs. When you print a dictionary, it outputs pairs of keys and values.

Let’s take a look at the best ways you can print a dictionary in Python.

Print dictionary

Content of a Python dictionary can be printed using the print() function.

cars = {"brand": "Toyota", "model": "Corolla", "year": 2018}

If you run the code, Python is going to return the following result:

{'brand': 'Toyota', 'model': 'Corolla', 'year': 2018}

Both keys and values are printed.

You can also use the dictionary method called items().

cars = {"brand": "Toyota", "model": "Corolla", "year": 2018}

This function will display key-value pairs of the dictionary as tuples in a list.

dict_items([('brand', 'Toyota'), ('model', 'Corolla'), ('year', 2018)])

Printing with the for loop

items() can be used to separate dictionary keys from values. Let’s use the for loop to print the dictionary line by line.

cars = {"brand": "Toyota", "model": "Corolla", "year": 2018}
for key, value in cars.items():
    print(key, value)

If you run the code, the key-value pair will be printed using the print() function.

brand Toyota
model Corolla
year 2018

Print keys and values separately

With the items() method, you can print the keys and values separately.

for keys:

for key, value in cars.items():

for values:

for key, value in cars.items():

Python offers additional methods keys() and values() methods to achieve the same result.

keys() method:

for key in cars.keys():

values() method:

for value in cars.values():

Using list comprehension to print dictionary

With a list comprehension, we can print a dictionary using the for loop inside a single line of code.

cars = {"brand": "Toyota", "model": "Corolla", "year": 2018}
[print(key, value) for key, value in cars.items()]

This code will return the contents of a dictionary line by line.

brand Toyota
model Corolla
year 2018

In a similar manner, you can also do list comprehension with keys() and values().

cars = {"brand": "Toyota", "model": "Corolla", "year": 2018}
[print(key) for key in cars.keys()]
[print(value) for value in cars.values()]



Prettyprint dictionaries as a table

If a dictionary becomes more complex, printing it in a more readable way can be useful. This code will display the dictionary as a table.

cars = {11: ["Toyota", "Corolla", 2018],
        2: ["Audi", "A6", 2014],
        4: ["Citroen", "C5", 2009],
        7: ["Ford", "Focus", 2017]}

print("{:<5} {:<10} {:<10} {:<10}".format("Key", "Brand", "Model", "Year"))

for key, value in cars.items():
    brand, model, year = value
    print("{:<5} {:<10} {:<10} {:<10}".format(key, brand, model, year))

Inside the new dictionary, four elements represent multiple cars. The first part is a key, the second part (value) is a list consisting of the brand of a car, model, and year of production.

The first print() function displays four headers: “Key”, “Brand”, “Model”, “Year”. Each of them is spaced by the number of characters from the previous column.

The same is done to the dictionary items. Each value is a list assigned to three variables: brand, model, and year, with the same amount of spacing.

If you run the code, you’ll see a dictionary displayed in a pretty tabular form.

Key   Brand      Model      Year      
11    Toyota     Corolla    2018      
2     Audi       A6         2014      
4     Citroen    C5         2009      
7     Ford       Focus      2017