Possible Duplicate:
How are Python’s Built In Dictionaries Implemented
I am fairly new to python and have a background in java. I was wondering whether the dictionary in python has the same search complexity as for hash maps in java. Example: searching for a key in a hash table/map in java is a constant time operation, I was wondering whether searching for a key in a dictionary in python is also a constant time operation. I have read through a few pages of python documentation regarding mapping but it doesn’t seem to indicate any hashing of keys of dictionary in python so I was wondering whether:
- searching keys in a dictionary in python was constant time operation.
- If so how do they achieve this constant time search without hashing?
Python dictionaries have O(1) search complexity.
See the Time complexity wiki page.
Python dictionaries are implemented as a hash table, and keys are hashed; you can influence the hashing by implementing a
__hash__method.