Python – Extracting Kth Key in Dictionary
Last Updated :
27 Apr, 2023
Many times, while working with Python, we can have a situation in which we require to get the Kth key of dictionary. There can be many specific uses of it, either for checking the indexing and many more of these kind. This is useful for Python version 3.8 +, where key ordering are similar as insertion ordering. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list() + keys() The combination of above methods can be used to perform this particular task. In this, we just convert the entire dictionaries’ keys extracted by keys() into a list and just access the Kth key.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ("The original dictionary is : " + str (test_dict))
K = 1
res = list (test_dict.keys())[K]
print ("The Kth key of dictionary is : " + str (res))
|
Output :
The original dictionary is : {'best': 3, 'Gfg': 1, 'is': 2}
The Kth key of dictionary is : Gfg
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using next() + iter() This task can also be performed using these functions. In this, we just take the Kth next key using next() and iter function is used to get the iterable conversion of dictionary items.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ("The original dictionary is : " + str (test_dict))
K = 1
test_dict = iter (test_dict)
for i in range ( 0 , K + 1 ) :
res = next (test_dict)
print ("The Kth key of dictionary is : " + str (res))
|
Output :
The original dictionary is : {'best': 3, 'Gfg': 1, 'is': 2}
The Kth key of dictionary is : Gfg
Using dictionary items method:
Approach:
In this approach, we use the items() method of the dictionary to extract a list of (key, value) pairs and then return the Kth key.
The function is called with two arguments: a dictionary and a value k.
The function creates a list of items in the dictionary using the items() method and the list() constructor: items_list = list(dictionary.items())
The function checks if the requested k is within the range of the number of items in the dictionary using an if statement: if k < len(items_list):
If k is within the range of the dictionary, the function returns the kth key using the first element of the kth item in the items list: return items_list[k][0]
If k is outside the range of the dictionary, the function returns None: else: return None
The function call returns the kth key, which is stored in the kth_key variable.
The print() function is called to display the result: print(“The Kth key of dictionary is:”, kth_key)
Python3
def get_kth_key_dict(dictionary, k):
items_list = list (dictionary.items())
if k < len (items_list):
return items_list[k][ 0 ]
else :
return None
dictionary = { 'best' : 3 , 'Gfg' : 1 , 'is' : 2 }
k = 1
kth_key = get_kth_key_dict(dictionary, k)
print ( "The Kth key of dictionary is:" , kth_key)
|
Output
The Kth key of dictionary is: Gfg
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...