2

Python dictionaries use hashing to get O(1) complexity for certain operations.

I know a hash is a function that converts an inmutable object into a unique identifier of a certain length of bits.

However I still dont' understand how certain operations work at a low level.

At a low level, I could understand that if the hash function would translate a key into a memory position, then getting the dictionary value for a certain key would be just hashing that key, going to that memory position and retrieving the value. But I suspect this is not how it really works, basically because the entire range of possible hashes would have to be allocated into memory.

Checking if a dictionary has a certain key is also O(1), and I really don't figure out how it's done to be O(1) using hashes. Sure I have a hash, now what? I still need to check if this hash value belongs to a list of hash values right? And this is not O(1).

Is there a tutorial or documentation where it really explains at a low level how the different operations of dictionaries involving hashes are done?

All tutorials I found explain how to use dictionaries, but I want to know how they work.

I'm not interested in the hashing algorithm itself, but what comes after the hash is calculated.

Thanks

  • This is entirely up to the implementation of Python. In CPython, for example, the hash is just an index into a C array. – chepner Sep 19 '19 at 15:23

0 Answers0