Python – Print Dictionary

Python – Print Dictionary

To print dictionary items: key:value pairs, keys, or values, you can use an iterator for the corresponding key:value pairs, keys, or values, using dict.items(), dict.keys(), or dict.values() respectively and call print() function.

In this tutorial, we will go through example programs, to print dictionary as a single string, print dictionary key:value pairs individually, print dictionary keys, and print dictionary values.

Print Dictionary as a Single String

To print whole Dictionary contents, call print() function with dictionary passed as argument. print() converts the dictionary into a single string literal and prints to the standard console output.

In the following program, we shall initialize a dictionary and print the whole dictionary.

Python Program

dictionary = {'a': 1, 'b': 2, 'c':3}
print(dictionary)
Run

Output

{'a': 1, 'b': 2, 'c': 3}

Print Dictionary Key:Value Pairs

To print Dictionary key:value pairs, use a for loop to traverse through the key:value pairs, and use print statement to print them. dict.items() returns the iterator for the key:value pairs and returns key, value during each iteration.

In the following program, we shall initialize a dictionary and print the dictionary’s key:value pairs using a Python For Loop.

Python Program

dictionary = {'a': 1, 'b': 2, 'c':3}

for key,value in dictionary.items():
	print(key, ':', value)
Run

Output

a : 1
b : 2
c : 3

Print Dictionary Keys

To print Dictionary keys, use a for loop to traverse through the dictionary keys using dict.keys() iterator, and call print() function.

In the following program, we shall initialize a dictionary and print the dictionary’s keys using a Python For Loop.

Python Program

dictionary = {'a': 1, 'b': 2, 'c':3}

for key in dictionary.keys():
	print(key)
Run

Output

a
b
c

Print Dictionary Values

To print Dictionary values, use a for loop to traverse through the dictionary values using dict.values() iterator, and call print() function.

In the following program, we shall initialize a dictionary and print the dictionary’s values using a Python For Loop.

Python Program

dictionary = {'a': 1, 'b': 2, 'c':3}

for value in dictionary.values():
	print(value)
Run

Output

1
2
3

Summary

In this tutorial of Python Examples, we learned how to print Dictionary, its key:value pairs, its keys or its values.