Is numeric keys of Python's dictionary automatically sorted?

Carsten Haese carsten at
Wed Mar 7 21:55:22 CET 2007

On Wed, 2007-03-07 at 15:18 -0500, John wrote:
> I am coding a radix sort in python and I think that Python's dictionary may 
> be a choice for bucket.
> The only problem is that dictionary is a mapping without order. But I just 
> found that if the keys are numeric, the keys themselves are ordered in the 
> dictionary.


The sequence of keys in a dictionary is a coincidental side effect of
the particular Python implementation, the number of keys, the values of
the keys, and the order in which the keys are inserted. You must not
rely on the keys appearing in any particular order.

Here is a simple counterexample that breaks the ordering, at least for
the version I'm running:

>>> d = {}
>>> for i in range(0,6): d[10**i] = []
>>> d
{100000: [], 1: [], 100: [], 1000: [], 10: [], 10000: []}


More information about the Python-list mailing list