'Efficient way of retreiving index of dictionary entry by key in Python

As I understand it, dictionaries in Python are ordered as of Python 3.7. Given a dictionary with N entries, I should be able to associate to each key an index from 0 to N-1. My question is, given a key, is there any way to retrieve this index in an efficient manner? It seems like there should be a more efficient way than retrieving the list of keys and searching for the specific key of interest.



Solution 1:[1]

One of the ways to do this is list(dict_name.keys()).index(key_name). Another way would be using operator.indexOf. I'm not sure why you would need the index of the keys in the first place, as getting a value from a dictionary is already O(1), or constant time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1