Concept
A hash function turns a key into an array index, which is why dictionaries find things in constant time. Two keys can hash to the same slot, and how the table handles that collision decides its real performance.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Hashing and collisions doing its job.
class HashSet:
def __init__(self, buckets=8):
self.buckets = [[] for _ in range(buckets)]
def add(self, key):
bucket = self.buckets[hash(key) % len(self.buckets)]
if key not in bucket: # chaining: a list per slot
bucket.append(key)
def __contains__(self, key):
return key in self.buckets[hash(key) % len(self.buckets)]Using a mutable object, such as a list, as a dictionary key. Its hash would change when it changes, so Python refuses with TypeError: unhashable type. Use a tuple.
Your roadmap puts these before Hashing and collisions, unless you already know them.
Once a session shows you understand Hashing and collisions, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.
Topics that build on Hashing and collisions.