Order of tuples in dict.items()

John Machin sjmachin at lexicon.net
Sun Oct 14 19:01:00 EDT 2007


On Oct 15, 8:27 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> I've never seen the point of a sorted dictionary, it's easy to just say:
>
> for key, value in sorted(D.items())

There are several applications that involve finding i such that key[i]
<= query < key[i+1] where the keys are sorted and unique ... perhaps
with a huge sentinel key at the end. Examples include sliding-scale
taxes, and looking up prices for shares or managed funds. The code to
do this in Python using the bisect module is about as clear as the
equivalent SQL :-)




More information about the Python-list mailing list