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.

Basic Example

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

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.

Some methods work in Python 2.x, and some are best for Python 3.x.

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.

  • 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

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 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. Print both keys and values directly.

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

Code

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.

  • The keys() method returns a new version of the dictionary keys
  • Use the view to iterate over the dictionary and print the key and value pair
  • Use the youdict[key] during each iteration to find the key’s value

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

Code

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.

  • For loop iterates over the dictionary keys.
  • During each iteration, access the key’s value using yourdict[key] and print it.

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

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

Printing First Key Value Pair Of A Dictionary

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

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.

From Python 3.6, dictionary key insertion orders are preserved.

  • Convert the dictionary keys to a list
  • Access the first item of the list using the index 0

Code

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

Code

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, the ValueError will be thrown 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,

  • 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,

  • Print the key and value and break the iteration.
  • Because dictionary keys are unique, there is no chance that the key will exist again.

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

Code

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.

To print them in reverse order

  • Use 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

Additional Resources

Leave a Comment