Dictionary sorting

Ben Finney ben+python at benfinney.id.au
Thu Nov 3 20:06:20 EDT 2011


Tim Chase <python.list at tim.thechases.com> writes:

> On 11/03/11 16:36, Terry Reedy wrote:
> > CPython iterates (and prints) dict items in their arbitrary internal
> > hash table order, which depends on the number and entry order of the
> > items. It is a bug to depend on that arbitrary order in any way.
>
> Does this "never trust it" hold even for two consecutive iterations
> over an unchanged dict? I didn't see anything in the docs[1] to make
> such a claim,

Exactly. The order of retrieval is entirely up to the implementation.
There is no guarantee that the order will be the same the next time you
iterate over the same dict. It is a bug to depend on a reliable order of
retrieving the items.

> I didn't know if what I saw was merely an implementation detail that
> shouldn't be counted on.

If the docs don't specify some particular observed behaviour in Python,
you should consider it an implementation detail.

-- 
 \       “Pray, v. To ask that the laws of the universe be annulled in |
  `\     behalf of a single petitioner confessedly unworthy.” —Ambrose |
_o__)                           Bierce, _The Devil's Dictionary_, 1906 |
Ben Finney



More information about the Python-list mailing list