How To Print Key Value Pairs of a Dictionary in Python -Definitive Guide

Python dictionaries allow you to store objects as key-value pairs.

You can print key-value pairs of a dictionary using the for key, val in yourdict.items() and print() method in Python.

This tutorial teaches you how to print key value pairs of a dictionary in Python using different methods and when it is appropriate to use them.

If You’re in Hurry…

You can iterate over a dictionary using a for loop and print the key-value pairs using the print() function.

This method works in both Python 2.x and Python 3.x.

Here the key is the variable name used to hold the key value during each iteration. You can use this key to fetch the value of the specific key.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key in yourdict:

    print(key, yourdict[key])

Output

    one 1
    two 2
    three 3
    four 4

If You Want to Understand Details, Read on…

Different methods are available to access the items of a dictionary. Let us learn each method in detail.
Some methods work in Python 2.x, and some are best for Python 3.x.

All the methods demonstrated here print the dictionary keys and values line by line.

Finding Python Version

You can check the python version using the below script.

Code

import sys
print(sys.version)

Output

3.8.2 (default, Sep  4 2020, 00:03:40) [MSC v.1916 32 bit (Intel)]

Now you know the Python version. You can use the below methods appropriately.

Using Dict.items() Method

Use this method if you use Python version 3.x or later.

You can use the dict.items() method to iterate over the dictionary items.

It returns a view object that contains key-value pairs of the dictionary as a list of tuples.

With the key-value pair view, you can iterate over the dictionary using the for loop and print each pair.

Code

The code below demonstrates how to use the items() method with the for loop.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key, val in yourdict.items():

    print(key, val)

Output

    one 1
    two 2
    three 3
    four 4

Using Dict.Iteritems() Method

Use this method ONLY if you use Python version 2.x. It is deprecated in Python 3.x.

You can use the dict.Iteritems() method to iterate over the dictionary items.

It returns an iterator that contains key-value pairs of the dictionary.

With the key-value pair iterator, you can iterate over the dictionary using the for loop and print each pair. Each iteration contains both keys and values. So you can print both keys and values directly.

Code

The code below demonstrates how to use the iteritems() method while using Python version 2.x.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key, val in yourdict.iteritems():

    print(key, val)

Output

one 1
two 2
three 3
four 4

Using Keys() Method

This section teaches you how to use the dictionary keys() method to print the key value pairs of the dictionary.

Use this method when you want to use or check the key before printing it—for example, printing a specific key-value pair.

The keys() method returns a new version of the dictionary keys.

You can use the view to iterate over the dictionary and print the key and value pair.

You must use the youdict[key] during each iteration to find the key’s value.

Code

The code below demonstrates how to use the keys() method to print the key-value pairs of the dictionary.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key in yourdict.keys():

    print(key, yourdict[key])

Output

    one 1
    two 2
    three 3
    four 4

Using For Loop With Dictionary

You can also directly iterate the dictionary object using the for loop.

This iterates over the dictionary keys. During each iteration, you need to access the key’s value using yourdict[key] and print it.

This is similar to the dict.keys() method.

Code

The code below demonstrates how to iterate over the dictionary object using the for loop and print the key-value pair.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key in yourdict:

    print(key, yourdict[key])

Output

    one 1
    two 2
    three 3
    four 4

Printing First Key Value Pair Of A Dictionary

No direct method is available to print the first key-value pair of a dictionary.

Also, the python dictionaries are not ordered until Python version 3.6. Hence, it doesn’t make sense to print the first key-value pair in the earlier version of Python.

But from Python 3.6, dictionary key insertion orders are preserved. Hence you can convert the dictionary keys to a list and access the first item of the list using the index 0.

Code

The code below demonstrates how to get and print the first key-value pair of the dictionary.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

first_key = list(yourdict)[0]

first_val = list(yourdict.values())[0]

print(first_key,first_val)

Output

    one 1

Printing Specific Key Value Pair

The best way to print specific key value pair of a dictionary is to check if the key exists in the dictionary. If it exists, then print the key-value pair.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

key == "one"

if key in yourdict:

    print(key, yourdict[key])

If you directly print the key-value pair before checking existence, then you’ll face the ValueError if the value doesn’t exist in the dictionary.

Print Specific Key Value Pair During Iteration

To print the specific key-value pair of the dictionary object, you need to check the key value during each iteration to see if the key is equal to your desired key value. If so, you can print it. Otherwise, continue the iteration.

Once you’ve found that the key is equal to the specific key, you can print the key and value and break the iteration. Because dictionary keys are unique, and there is no chance that the key will exist again.

With the iteration break, you can save some time complexity of the iteration.

Code

The code below demonstrates how to print a specific key value.

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key in yourdict.keys():

    if key == "one":

        print(key, yourdict[key])

        break

Output

    one 1

Printing Key value in Reverse Order

If you’re printing Key-Value Pairs in Python version 3.6 or later, they are printed in the order of the key insertion.

You can also print them in reverse order using the sorted() function and reverse=True parameter.

The keys insertion order will be reversed and printed.

Code

yourdict = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
}

for key, val in sorted(yourdict.items(), reverse=True):

    print(key, val)

Output

    two 2
    three 3
    one 1
    four 4

Conclusion

You’ve learned how to print key value pairs of a dictionary in Python. Additionally, you’ve learned which method to use when using Python version 2.x and which method to use Python 3.x.

The print() method is used to print the values, and it prints the dictionary keys and values line by line.

You May Also like

Leave a Comment