0

Simple question.

What hashing algorithm is used in python's default dict?

>>> g = { 'a' : 1, 'b' : 2, 'c' : 3 }
>>> g
{'a': 1, 'c': 3, 'b': 2}
>>> g.keys()
['a', 'c', 'b']
>>>

I expected ['a','b','c'] on g.keys() Linear probe (guess not)? double hash?

5
  • 4
    hg.python.org/cpython/file/tip/Objects/dictobject.c might contain something relevant. Commented Mar 28, 2013 at 23:46
  • Any hashing is size dependent. And the char/string hash is used. This should not be surprising. Commented Mar 28, 2013 at 23:46
  • @riamse That code is well commented. Commented Mar 28, 2013 at 23:48
  • 2
    It's impossible to know what the "order" will be without knowing the hash function, the bucket count at each insertion, the collision resolution, and the order of insertion .. at the very least. The linked source code says how a particular implementation does it. These same "unpredictable results" can be achieved through chaining, probing, double hashing, and extensible hashing - that is, the result is a compound factor and not directly related to the collision resolution. Commented Mar 28, 2013 at 23:49
  • Why did you expect a, b, c, knowing it was a hash? Linear probe is frowned upon. Commented Mar 28, 2013 at 23:51

1 Answer 1

3

There is no guarantee that Python will use any particular method - different implementations could use any one they wish. dicts are unordered, so it doesn't matter how it's implemented (provided it fulfills certain obligations).

As to how CPython does it...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.