Java – among other languages – has a TreeMap
data structure. This map allows us to keep the entries sorted based on a given key. I find this scenario very common and I consider Java quite “considerate” for providing a data structure like this out-of-the-box.
On the other side, Python does not provide such a structure in its main specifications. There are a few semi-solutions out there:
- There’s
SortedDict
from thesortedcontainers
package that we have to install. - There’s
OrderedDict
from the main specification that only respects insertion order – likedict()
but the user needs to sort entries themselves. - One can use a
heapq
of tuples but this does not maintain uniqueness or provide O(1) access.
I have read a few discussions out there on the topic but unfortunately, most of them revolve around the notion of not-needing-it. I would argue that when a data structure is able to model such a common scenario, it’s more than worthy of being added to the main specification of a language.
Why did then Python decide to exclude such an integral data structure from its main specification?