Best way to hash a dictionary

Anders J. Munch andersjm at
Wed Feb 26 16:50:17 CET 2003

"Miki Tebeka" <tebeka at> wrote:
> Hello All,
> I need to store dictionary as dictionary keys, and do it efficiently
> (which means using native types).
> Currently there are 3 ways I can think of: 
> 1. Using a sorted tuple of dictionary items
> 2. Using a sorted string from the dictioary items
> 3. Using a string from the dicionary items (AFAIK this is a bug)
> From my test #1 is the fastet. (See below)
> Any other suggestions (are there immutable dictionaries?)?

Compute the hash as the xor of the hash of the items.  That way you
don't need to sort first, and you'll get an O(n) hash instead of

but-before-that-give-heed-to-Steve's-advice-ly y'rs, Anders

More information about the Python-list mailing list