Loading notes...
Loading notes...
B.Tech • Chapter 4
B.Tech level module on Hashing.
Dictionaries and Sets are implemented using Hash Tables. They provide O(1) average time complexity for lookups, insertions, and deletions, making them arguably the most important data structures in Python.
Advanced Engineering Concepts
A hash table uses a hash function to compute an index into an array of buckets. In Python, dictionaries are ordered (as of Python 3.7) by keeping a separate dense array for insertion order and a sparse array for the hash table. Hash collisions are resolved using open addressing with quadratic probing. Tuples are immutable and therefore hashable, meaning they can be used as dictionary keys, whereas lists cannot.
Code Implementation
# Dictionary implementation and Hashing
my_dict = {"id": 101, "name": "Engineer"}
# Tuples as keys
route_distances = {
("Delhi", "Mumbai"): 1400,
("Bangalore", "Chennai"): 350
}
print(hash(("Delhi", "Mumbai")))
# print(hash(["Delhi", "Mumbai"])) # This will raise TypeError: unhashable type: 'list'