[Christopher Barker <python...@gmail.com>] > Maybe a stupid question: > > What are use cases for sorted dicts? > > I don’t think I’ve ever needed one.
For example, for some mappings with totally ordered keys, it can be useful to ask for the value associated with a key that's not actually there, because "close to the key is good enough". A SortedDict supports several ways of doing that efficiently, depending on what - exactly - an app means by "good enough". For example, it's easy to find _the_ closest key. Or the k closest keys. Or the two single keys <= and >=. Or ... Pick one; take some form of average of their values; use a domain-specific interpolation function on their keys & values; ... To make it concrete, suppose you're running a COVID-19 app where reports of new cases are coming in around the clock. One SortedDict may be used to map latitude to the number of confirmed cases reported. If a researcher asks for the number at a specific latitude, but no exact match is found, fine, you can still efficiently show them the results across the smallest known interval containing the latitude of interest. Or if they want the results broken into buckets of latitudes in 15-degree (N-degree - any N they ask for) intervals, also easy: enumerating all entries in a given key range also goes fast. Or ... A sorted list supports all sorts of membership queries efficiently. Under the covers, a SortedDict _is_ a SortedList, plus a regular Python dict to associate values with the list's elements. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FVDQZ4FJMSPYYTKNDRGLUXMOE226YIDK/ Code of Conduct: http://python.org/psf/codeofconduct/