Python: Print Specific key-value pairs of dictionary

In this article, we will discuss different ways to print specific key-value pairs of a dictionary. We can either use indexing or conditions to select few pairs from a dictionary and print them.

Table of Contents

Print specific key-value pairs from dictionary using indexing

The items() function of dictionary returns an iterable sequence of key-value pairs of dictionary i.e. dict_items. But this is view only and we can not use indexing on this sequence. So, if we need to select items from a dictionary using indexing, then we need to create a list of pairs from this sequence. For example,

Print first key-value pair of dictionary

# Dictionary of string and int
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Create a list of all key-value pairs of dictionary
all_pairs = list(word_freq.items())

print('First Key value pair: ', all_pairs[0])

Output:

First Key value pair:  ('Hello', 56)

Print last key-value pair of dictionary

# Dictionary of string and int
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Create a list of all key-value pairs of dictionary
all_pairs = list(word_freq.items())

print('Last Key value pair: ', all_pairs[-1])

Output:

Advertisements
Last Key value pair:  ('Why', 11)

Print Nth key-value pair of dictionary

# Dictionary of string and int
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Create a list of all key-value pairs of dictionary
all_pairs = list(word_freq.items())

n = 3
print('3rd value pair: ', all_pairs[n-1])

Output:

3rd value pair:  ('test', 43)

If you a big dictionary and creating a list of all key-value pairs of dictionary seems too bad from performance point of view. Then you can also iterate over all the pairs of dictionary by index using the enumerate() function and print the element at the given index. For example,

# Dictionary of string and int
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

n = 2
# Iterate over all pairs of dictionary by index and 
# print the pair at index n
for index, (key, value) in enumerate(word_freq.items()):
    if index == n:
        print(key, '::', value)
        break

Output:

test :: 43

It printed the key-value pair at index position N.

Print specific key-value pairs of dictionary based on conditions

To print specific items of dictionary which satisfy a condition, we can iterate over all pairs of dictionary and for each pair check the condition. If condition returns True then print the pair else just skip it. For example, let’s print all the key-value pairs of dictionary whose value is an even number,

# Dictionary of string and int
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Print key-value pairs in dictionary whose value is even
for key, value in word_freq.items():
    if value % 2 == 0:
        print(key, '::', value)

Output:

Hello :: 56
This :: 78

It printed the key-value pairs with even values.

Summary

We learned how to print specific key-value pairs of a dictionary in python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top