I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+.
Given they are ordered, it seems strange that no methods exist to retrieve the ith item of a dictionary by insertion order. The only solutions available appear to have O(n) complexity, either:
- Convert to a list via an O(n) process and then use
list.__getitem__
. enumerate
dictionary items in a loop and return the value when the desired index is reached. Again, with O(n) time complexity.
Since getting an item from a list
has O(1) complexity, is there a way to achieve the same complexity with dictionaries? Either with regular dict
or collections.OrderedDict
would work.
If it's not possible, is there a structural reason preventing such a method, or is this just a feature which has not yet been considered / implemented?