How Do Dictionaries Work in Python?
Learn how Python dictionaries work: key-value pairs, hash tables, O(1) lookup, hashable keys, get vs bracket access, order, examples, and interview questions.
Expected Interview Answer
A Python dictionary is a built-in collection of key-value pairs that stores data by key rather than by position, giving average O(1) lookup, insertion, and deletion through an underlying hash table.
Each key must be unique and hashable (immutable types like strings, numbers, or tuples), while values can be any object. Python hashes the key to compute where the pair is stored, which is why access is fast and does not depend on the number of items. Since Python 3.7 dictionaries also preserve insertion order, and you can access values with square-bracket lookup or the safer get method.
- Fast average O(1) lookup by key
- Clear mapping of meaningful keys to values
- Flexible values of any type
- Insertion order preserved since Python 3.7
- Rich methods like get, keys, values, items, and update
AI Mentor Explanation
A dictionary is like a team sheet mapping each jersey number to a player: you look up number 18 and instantly get the batter, without scanning the whole squad. The jersey number is the unique key and the player is the value, and just as no two players share a number, no two dictionary keys can be the same.
Step-by-Step Explanation
Step 1
Create a dictionary
Use curly braces with key: value pairs, e.g. person = {"name": "Ana", "age": 30}.
Step 2
Access by key
Read a value with person["name"], or use person.get("name") to avoid a KeyError on missing keys.
Step 3
Add or update
Assigning person["city"] = "Rome" adds a new pair or overwrites an existing key's value.
Step 4
Hashing under the hood
Python hashes each key to decide where the pair is stored, giving average O(1) access.
Step 5
Iterate
Loop with for key, value in person.items() to visit every pair in insertion order.
What Interviewer Expects
- Defines a dictionary as key-value pairs backed by a hash table
- Knows keys must be unique and hashable
- States average O(1) lookup performance
- Mentions get versus square-bracket access
- Aware that insertion order is preserved since Python 3.7
Common Mistakes
- Trying to use a mutable object like a list as a key
- Assuming dictionaries are unordered in modern Python
- Using dict[key] and getting a KeyError instead of using get
- Confusing keys() and values() or thinking values must be unique
Best Answer (HR Friendly)
“A Python dictionary stores information as pairs of a key and a value, like a contact list that maps a name to a phone number. You look things up by the key, which is very fast, and each key is unique so it always points to one value.”
Code Example
# Create a dictionary of key-value pairs
person = {"name": "Ana", "age": 30}
# Access by key
print(person["name"]) # Ana
# Safe access avoids KeyError on a missing key
print(person.get("city")) # None
print(person.get("city", "NA")) # NA
# Add or update a pair
person["city"] = "Rome"
person["age"] = 31
# Iterate over pairs (insertion order is preserved)
for key, value in person.items():
print(key, "->", value)
# Keys must be hashable; a list key raises TypeError
# bad = {[1, 2]: "x"} # TypeError: unhashable type: 'list'Follow-up Questions
- What types can be used as dictionary keys, and why?
- What is the time complexity of a dictionary lookup?
- How does dict.get() differ from square-bracket access?
- Are Python dictionaries ordered? Since which version?
- How would you merge two dictionaries in Python 3.9+?
MCQ Practice
1. What is the average time complexity of a dictionary lookup by key?
Dictionaries use a hash table, so lookup, insertion, and deletion by key are average O(1).
2. Which of these is a valid dictionary key?
Keys must be hashable. A tuple of hashable items works, but lists and sets are unhashable.
3. What does person.get("city") return if the key is missing?
get() returns None for a missing key by default, avoiding the KeyError that person["city"] would raise.
Flash Cards
What is a Python dictionary? — A collection of unique key-value pairs backed by a hash table, with average O(1) lookup.
What can be a dictionary key? — Any hashable (usually immutable) object: strings, numbers, or tuples of hashables — not lists.
get() vs dict[key]? — get() returns None (or a default) for a missing key; dict[key] raises KeyError.
Are dicts ordered? — Yes — insertion order is preserved as of Python 3.7.