Finding first key-value pair from a Dictionary in Python
In this article, we will see of different methods by which we can fetch the first key-value pair of a dictionary. We will also discuss how to get first N pairs and any number of key-value pairs from a dictionary.
Getting first key-value pair of dictionary in python using iter() & next() :
In python, iter()
function creates a iterator object of the the iterable sequence of key-value pairs from dictionary and by calling next() function we can get the first key-value pair.
# Program : dict_eg = { 'Sachin' : 10, "Gayle" : 333, 'Kohil' : 18, 'Murali' : 800, 'Dhoni' : 7, 'AB' : 17 } # Get the first key-value pair in dictionary dict_eg = next(iter((dict_eg.items())) ) print('The first Key Value Pair in the Dictionary is:') print(dict_eg) print('First Key: ', dict_eg[0]) print('First Value: ', dict_eg[1])
Output : The first Key Value Pair in the Dictionary is: ('Sachin', 10) First Key:Â Sachin First Value:Â 10
Get first key-value pair of dictionary using list :
In python, items()
function in dictionary returns the iterable sequence of all key-value pairs. Then by creating a list from all key-value pairs in dictionary and by selecting the first item we will get first key-value pair of dictionary.
# Program : dict_eg = { 'Sachin' : 10, "Gayle" : 333, 'Kohil' : 18, 'Murali' : 800, 'Dhoni' : 7, 'AB' : 17 } # Get the first key-value pair in dictionary dict_eg = list(dict_eg.items())[0] print('First Key Value Pair of Dictionary:') print(dict_eg) print('Key: ', dict_eg[0]) print('Value: ', dict_eg[1])
Output : First Key Value Pair of Dictionary: ('Sachin', 10) Key:Â Sachin Value:Â 10
Getting the first N key-value pair of dictionary in python using list & slicing :
Here from similar process, we will create a list of key-value pairs from dictionary. We can get first ‘N’ items by list[:N]
or any items by list[start:end]
.
# Program : dict_eg = { 'Sachin' : 10, "Gayle" : 333, 'AB' : 17, 'Murali' : 800, 'Dhoni' : 7, 'AB' : 17, 'Kohil' : 18 } n = 5 # Get first 5 pairs of key-value pairs firstN_pairs = list(dict_eg.items())[:n] print('The first 5 Key Value Pairs of Dictionary are:') for key,value in firstN_pairs: print(key, '::', value)
Output : The first 5 Key Value Pairs of Dictionary are: Dhoni :: 7 Kohil :: 18 Gayle :: 333 Sachin :: 10 AB :: 17
Getting the first N key-value pair of dictionary in python using itertools :
We can slice first ‘N’ entries from a sequence by itertools.islice(iterable, stop)
after creating key-value pairs sequence from items() function.
# Program : import itertools dict_eg = { 'Sachin' : 10, "Gayle" : 333, 'AB' : 17, 'Murali' : 800, 'Dhoni' : 7, 'AB' : 17, 'Kohil' : 18 } n = 5 # Get first 5 pairs of key-value pairs firstN_pairs = itertools.islice(dict_eg.items(), n) print('The first 5 Key Value Pairs of Dictionary are:') for key,value in firstN_pairs: print(key, '::', value)
Output : The first 5 Key Value Pairs of Dictionary are: Murali :: 800 AB :: 17 Sachin :: 10 Dhoni :: 7 Kohil :: 18