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 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)) |
The original dictionary is : {'have': 4, 'all': 1, 'good': 3, 'food': 2}
Index of search key is : [2]
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 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)) |
The original dictionary is : {'food': 2, 'have': 4, 'good': 3, 'all': 1}
Index of search key is : 2
Recommended Posts:
- Python | Dictionary with index as value
- Python | Initializing dictionary with list index-values
- Python | Initializing dictionary with list index values
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python Dictionary
- Python | Get key from value in Dictionary
- Python | Get the first key in dictionary
- Python | Add new keys to a dictionary
- Python Dictionary clear()
- Python dictionary | values()
- Python | Nested Dictionary
- Python Dictionary Comprehension
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



