Saturday, December 28, 2024
Google search engine
HomeLanguagesPython | Key index in Dictionary

Python | Key index in Dictionary

The concept of dictionary is similar to that of map data structure in C++ language, but with the exception that keys in dictionary has nothing to do with its ordering, i.e it is not sorted unlike C++ in which the keys are sorted internally. This opens up the problem in which we might have to find the exact position of key in the dictionary. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + enumerate() The combination of above functions together can perform this particular task. In this, first the dictionary is converted to a pair tuple and then the first element of tuple being the key is checked for index. 

Python3




# Python3 code to demonstrate working of
# Key index in Dictionary
# Using list comprehension + enumerate()
 
# initializing dictionary
test_dict = {'all' : 1, 'food' : 2, 'good' : 3, 'have' : 4}
 
# initializing search key string
search_key = 'good'
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using list comprehension + enumerate()
# Key index in Dictionary
temp = list(test_dict.items())
res = [idx for idx, key in enumerate(temp) if key[0] == search_key]
 
# printing result
print("Index of search key is : " + str(res))


Output : 

The original dictionary is : {'have': 4, 'all': 1, 'good': 3, 'food': 2}
Index of search key is : [2]

Time complexity: O(n*n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

  Method #2 : Using list() + keys() + index() The combination of above functions can also be used to perform this particular task. In this, the dictionary keys are first converted to list and then found the desired one using the index method. 

Python3




# Python3 code to demonstrate working of
# Key index in Dictionary
# Using list() + keys() + index()
 
# initializing dictionary
test_dict = {'all' : 1, 'food' : 2, 'good' : 3, 'have' : 4}
 
# initializing search key string
search_key = 'good'
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using list() + keys() + index()
# Key index in Dictionary
res = list(test_dict.keys()).index(search_key)
 
# printing result
print("Index of search key is : " + str(res))


Output : 

The original dictionary is : {'food': 2, 'have': 4, 'good': 3, 'all': 1}
Index of search key is : 2

Method#3: for loop 

Approach

 uses a for loop to iterate through the keys of the dictionary and find the index of the search key.

Algorithm

1. Create a dictionary dict1 with key-value pairs.
2. Initialize the search key and index to None.
3. Iterate through the dictionary to find the index of the search key using a for loop.
4. When the search key is found, assign the index to a variable and break the loop.
5. Print the index of the search key.

Python3




# initialize dictionary
dict1 = {'have': 4, 'all': 1, 'good': 3, 'food': 2}
 
# initialize search key and index
search_key = 'good'
index = None
 
# iterate through the dictionary to find the index of the search key
for i, key in enumerate(dict1.keys()):
    if key == search_key:
        index = i
        break
 
print("Index of search key is : {}".format([index]))


Output

Index of search key is : [2]

Time Complexity: O(n), where n is the number of elements in the dictionary.

Auxiliary Space: O(1), as we are not using any extra memory.

RELATED ARTICLES

Most Popular

Recent Comments