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.
- Print specific key-value pairs of dictionary based on conditions.
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:
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,
Frequently Asked:
- What is a dictionary in python and why do we need it?
- Python – Initialize a Dictionary with default value
- How to Create a Dictionary from two Lists in Python?
- Python: Print all keys of a dictionary
# 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.