How can I instruct python to store, internally, a pre-hashed version of my strings, so that it will use that value when I perform dict/set lookups using my string as a key?
I remember reading about it some weeks ago, but can't find it in python docs at the moment :-/
When it comes to 10,000,000 items a dictionary lookup can be 585714 times faster than a list lookup. 6.6 or 585714 are just the results of a simple test run with my computer. These may change in other cases.
Python Dictionaries are fast but their memory consumption can also be high at the same time.
Dictionaries in Python are implemented using hash tables. It is an array whose indexes are obtained using a hash function on the keys.
defaultdict. defaultdict handles our previous error by building on top of dictionaries. It does not give a KeyError like the regular dictionaries. However, whenever you try to access or “assign to” a key that is not there, it will create that key and give it a default value that was already specified .
String interning is probably what you're thinking of.
See sys.intern
in Python 3
See intern
in Python 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With