Order of tuples in dict.items()

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Oct 16 01:07:34 CEST 2007

On Mon, 15 Oct 2007 14:11:27 -0700, John Machin wrote:

> On Oct 16, 12:47 am, Erik Jones <e... at myemma.com> wrote:
>> Not between two consecutive reads, no.  However, after any resizing of
>> a dict the result of Python's hash function for any given newly
>> inserted key is extremely likely to be different than it would have
>> been before the resizing, i.e. the method may be the same, but the
>> result is different.
> Could you please supply the basis for the above assertion? My reading of
> the docs for the built-in hash function, the docs for an object's
> __hash__ method, and the source (dictobject.c, intobject.c,
> stringobject.c) indicate (as I would have expected) that the hash of an
> object is determined solely by the object itself, not by the history of
> insertion into a dict (or multiple dicts!?).
> Note that position_in dict = some_function(hash(obj), size_of_dict) ...
> perhaps you are conflating two different concepts.

The hash() function doesn't even take a dictionary as an argument -- it 
simply can't be dependent on the history of insertions into the 
dictionary, because it can't know what dictionary to look at!

But as you say, the position in the dictionary itself depends on the 
result of the hash function, and the size of the dictionary, and what's 
already in the dict (that is to say, the history of insertions and 
deletions). That's how hash tables work.


More information about the Python-list mailing list