W3docs

How do I print the key-value pairs of a dictionary in python

You can use the items() method to print the key-value pairs of a dictionary in Python.

You can use the items() method to print the key-value pairs of a dictionary in Python. Here's an example code snippet:

Print the key-value pairs of a dictionary in Python

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

for key, value in my_dict.items():
    print(key, value)

This will output:

key-value pair output

a 1
b 2
c 3

You can also use the for loop to iterate over the keys of the dictionary and then use the key to access the corresponding value. Here's an example code snippet:

Iterate over the keys of the dictionary in Python

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

for key in my_dict:
    print(key, my_dict[key])

This will also give the same output as the previous example

iteration output

a 1
b 2
c 3