← Data Structures
3 / 25
Lesson 3 — Hash map
Hash map: look things up by key
A hash map stores key → value pairs and finds a value by its key almost instantly, no matter how many pairs there are (O(1) on average). In Python it is the dict. d.get(key, 0) returns the value for key, or 0 if the key is missing — perfect for counting.
freq = {}
freq["cat"] = freq.get("cat", 0) + 1 # {'cat': 1}
Why it matters: counting words, caching results and indexing users by ID — dicts are the most used data structure in Python.
Want it another way?
Task
Count how many times each word appears. Expected: {'the': 3, 'cat': 1, 'dog': 1}.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.