Loading

Quipoin Menu

Learn • Practice • Grow

python / Dictionaries
interview

Q1. What is a dictionary in Python?
A dictionary is an unordered, mutable collection of key-value pairs. Keys must be unique and hashable (immutable), values can be any type. Created with curly braces or dict(). Example:
d = {"name": "Alice", "age": 30}
Access values via keys: d["name"].

Q2. How do you add and remove items from a dictionary?
Add by assignment: d["key"] = value. Remove using del d["key"] or pop(key) which returns the value. Clear all with clear(). Example:
d["city"] = "NYC" value = d.pop("age") del d["name"]

Q3. What are the methods to iterate over a dictionary?
You can iterate over keys, values, or items (key-value pairs). Example:
for key in d: print(key, d[key]) for value in d.values(): print(value) for key, value in d.items(): print(key, value)

Q4. What is the difference between dict.get() and direct access?
Direct access (d[key]) raises KeyError if the key does not exist. get(key, default) returns default (or None) if key missing, avoiding exceptions. Example:
value = d.get("missing", "default")

Q5. What are dictionary comprehensions?
Similar to list comprehensions, they create dictionaries. Example:
squares = {x: x**2 for x in range(5)}
This creates a mapping from numbers to their squares.