As of Python 3.7, dictionaries are guarantueed to keep insertion order. According to the original proposal for the change of memory layout (https://mail.python.org/pipermail/python-dev/2012-December/123028.html), an indices list stores the index of each value, indexed by the key hash. Therefore, it should be possible to efficiently retrieve an index of an item in the dictionary by its key. However, there seems to be no method for doing that. The only way I can think of is the following:
a = {}
a[1] = "b"
a[0] = "a"
a[2] = "c"
list(a).index(0) # returns 1
However, this is very inefficient. Do I have to use a library with a custom dictionary implementation or is there a way to access the internal CPython representation of a dictionary?