In this article, we will discuss different ways to fetch first key-value pair from a dictionary. Then we will also learn about a way to get first N key-value pairs from a python dictionary.
Table of Contents
- Get first key-value pair of dictionary in python using iter() & next().
- Get first key-value pair of dictionary using list.
- Get first N key-value pair of dictionary in python using list & slicing.
- Get first N key-value pair of dictionary in python using itertools.
Get first key-value pair of dictionary in python using iter() & next()
The items() function of dictionary returns an iterable sequence of all key-value pairs of the dictionary. We can create an iterator object of this iterable sequence of key-value pairs using iter() function. The iter() function accepts a sequence as argument and returns an iterator. Then by calling the next() function with this iterator object, we can get the first element of this sequence i.e. first key-value pair of the dictionary. For example,
# A dictionary of string and integers word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Get first key-value pair of the dictionary first_pair = next(iter((word_freq.items())) ) print('First Key Value Pair of Dictionary:') print(first_pair) print('First Key: ', first_pair[0]) print('First Value: ', first_pair[1])
Output:
First Key Value Pair of Dictionary: ('Hello', 56) First Key: Hello First Value: 56
We fetched the iterable sequence of key-vaue pairs of dictionary and then selected the first key-vaue pair from the dictionary.
Get first key-value pair of dictionary using list
As items() function of the dictionary class returns an iterable sequence of all key-value pairs of the dictionary. We can cast that sequence to a list and then select first item from that list. That will be our first key-value pair from the dictionary. For example,
# A dictionary of string and integers word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } # Get first key-value pair of the dictionary first_pair = list(word_freq.items())[0] print('First Key Value Pair of Dictionary:') print(first_pair) print('Key: ', first_pair[0]) print('Value: ', first_pair[1])
Output:
Frequently Asked:
- How to get first key in Dictionary – Python
- Python – Initialize a Dictionary with default value
- Python | Add dictionary to dictionary without overwriting
- Create a Dictionary from two Lists in Python
First Key Value Pair of Dictionary: ('Hello', 56) Key: Hello Value: 56
We created a list of all key-value pairs from the dictionary. Then selected the first item from list, which is the first key-value pair of the dictionary.
Related Stuff:
- Solved- TypeError: dict_keys object does not support indexing
- Solved- TypeError: dict_values does not support indexing
- Python: Get first value in a dictionary
- How to get first key in Dictionary – Python
- Get first key-value pair from a Python Dictionary
Get first N key-value pair of dictionary in python using list & slicing
Create a list of all key-value pairs from the dictionary, as we did in the previous example. Then select first N entries from the list by slicing ( list[start:end] ) i.e. to select first N entries, slice it using list[:N]. Let’s see an example, where we will select first 3 key-value pairs from a dictionary,
# A dictionary of string and integers word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } n = 3 # Get first 3 key-value pairs of the dictionary first_n_pairs = list(word_freq.items())[:n] print('First 3 Key Value Pairs of Dictionary:') for key,value in first_n_pairs: print(key, '::', value)
Output:
First 3 Key Value Pairs of Dictionary: Hello :: 56 at :: 23 test :: 43
We created a list of all key-value pairs from the dictionary. Then selected first 3 items from that list, which are the first 3 key-value pairs of the dictionary.
Get first N key-value pair of dictionary in python using itertools
In Python, the items() function of dictionary returns an iterable sequence of all key-value pairs of the dictionary. Then using the itertools.islice(iterable, stop), we can slice first N entries from the sequence. Let’s see an example, where we will select first 3 key-value pairs from a dictionary,
import itertools # A dictionary of string and integers word_freq = { 'Hello' : 56, "at" : 23, 'test' : 43, 'This' : 78, 'Why' : 11 } n = 3 # Get first 3 key-value pairs of the dictionary first_n_pairs = itertools.islice(word_freq.items(), n) print('First 3 Key Value Pairs of Dictionary:') for key,value in first_n_pairs: print(key, '::', value)
Output:
First 3 Key Value Pairs of Dictionary: Hello :: 56 at :: 23 test :: 43
The islice() function returns an iterator, whose next() method returns selected values from an iterable. To select the first 3 entries from sequence returned by items(), we passed the sequence and 3 as arguments in the islice() function. Therefore it returned an iterator to a sliced sequence containing first 3 key-value pairs of the original dictionary.
Conclusion:
Here, we learned about different ways to select firs key-value pair from a python dictionary. Then we also looked at ways to select first N pairs from the dictionary.