What is a Dictionary in Python?
Learn what a Python dictionary is, how its hash table works, why lookups are O(1), key requirements, and common interview questions with examples.
Expected Interview Answer
A dictionary (dict) is Python's built-in mapping type that stores data as key-value pairs, letting you look up a value almost instantly by its unique key instead of scanning through a list, and since Python 3.7 it also preserves the order items were inserted in.
Internally a dict is a hash table: each key is passed through a hash function to compute a slot, so lookups, insertions, and deletions run in average O(1) time regardless of how many items are stored. Keys must be hashable and therefore immutable (strings, numbers, tuples of hashable items), while values can be any type, including other dicts or lists. Dicts are created with curly braces {key: value} or the dict() constructor, and support methods like .get(), .keys(), .values(), .items(), and the merge operator |. Because they are so central to Python, many other features, like keyword arguments, class attributes, and JSON, are implemented on top of dicts.
- Near-constant-time lookup, insertion, and deletion by key
- Preserves insertion order (guaranteed since Python 3.7)
- Flexible values — any type, including nested dicts/lists
- Rich built-in methods (.get, .items, .update, merge with |)
- Foundation for kwargs, class __dict__, and JSON interchange
AI Mentor Explanation
A dictionary is like a scorecard app where you type a player's name and instantly see their stats, instead of scrolling through the whole team sheet to find them. The player's name is the key, their stats are the value, and the app finds the entry in a snap no matter how big the squad list gets.
Step-by-Step Explanation
Step 1
Key-value storage
A dict stores data as pairs; you retrieve a value by its unique key instead of a numeric position.
Step 2
Hash table internals
Each key is hashed to compute a storage slot, giving average O(1) lookup, insert, and delete.
Step 3
Key constraints
Keys must be hashable, so they need to be immutable types like str, int, or tuple.
Step 4
Insertion order
Since Python 3.7, dicts guarantee that iteration follows the order keys were inserted.
Step 5
Common operations
Use d[key], d.get(key, default), d.items(), d.update(), and the | merge operator for combining dicts.
What Interviewer Expects
- Describes dict as a hash table mapping keys to values
- States average O(1) time complexity for core operations
- Knows keys must be hashable/immutable
- Mentions guaranteed insertion order since Python 3.7
- Can use .get() to avoid KeyError safely
Common Mistakes
- Using d[key] instead of d.get(key) and crashing on missing keys
- Trying to use a list or another dict as a key
- Assuming dict order was never guaranteed (true before 3.7, not after)
- Confusing dict with set, which has no values, only unique keys
- Not knowing dict comprehensions exist: {k: v for k, v in pairs}
Best Answer (HR Friendly)
“A dictionary is Python's way of storing information as labeled pairs, like a name paired with a phone number, so you can look up any value instantly just by knowing its label. It is one of the most-used tools in Python because almost any real-world data — user profiles, settings, records — naturally fits this label-and-value shape.”
Code Example
user = {"id": 101, "name": "Ada", "role": "engineer"}
print(user["name"]) # Ada
print(user.get("email", "n/a")) # n/a (safe default)
user["email"] = "[email protected]" # add a new key
for key, value in user.items():
print(key, "->", value)
# id -> 101
# name -> Ada
# role -> engineer
# email -> [email protected]Follow-up Questions
- How does Python resolve hash collisions in a dict?
- What is the time complexity of dict lookup in the worst case?
- How do dict comprehensions work?
- What is the difference between dict.get() and dict[key]?
- How would you merge two dictionaries in modern Python?
MCQ Practice
1. What is the average time complexity of a dictionary lookup?
Dictionaries are hash tables, giving average O(1) time for lookup, insertion, and deletion.
2. Since which Python version do dicts guarantee insertion order?
Insertion-order preservation became a guaranteed language feature in Python 3.7 (it was a CPython implementation detail in 3.6).
3. Which of these can be used as a dictionary key?
Tuples of hashable elements are hashable and can be dict keys; list, dict, and set are all unhashable.
Flash Cards
What data structure backs a Python dict? — A hash table.
Average time complexity of dict lookup? — O(1) on average.
What must a dict key be? — Hashable, which means immutable (e.g., str, int, tuple).
Since when do dicts preserve insertion order? — Guaranteed since Python 3.7.