Python Dictionaries
Introduction
A Python dictionary (dict) is a mutable, unordered (historically) collection of key-value pairs.
(Note: As of Python 3.7, dictionaries maintain insertion order, but you should rarely rely on this property in algorithm interviews unless specifically using collections.OrderedDict).
Creating Dictionaries
You can create dictionaries using curly braces {} or the dict() constructor.
# Empty dictionary
empty_dict = {}
# Initialized dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
Accessing and Updating Values
You access values using square bracket notation.
If you access a key that does not exist, Python raises a KeyError.
To update a value or add a new key-value pair, assign a value to the key.
The in Operator (Crucial for Interviews)
To check if a key exists in a dictionary, use the in operator.
This is an \(O(1)\) operation and is the most common way to prevent KeyErrors.
Common Interview Mistake: Checking Values
The in operator only checks keys, not values.
val in person.values(), which takes \(O(N)\) time.
Deleting Elements
You can remove elements using the del keyword or the pop() method.
# Using del (raises KeyError if key doesn't exist)
del person["city"]
# Using pop (returns the value, allows a default to avoid KeyError)
job = person.pop("job", "Unemployed")
Time and Space Complexity
- Time Complexity:
- Insert: \(O(1)\) average
- Lookup: \(O(1)\) average
- Delete: \(O(1)\) average
key in dict: \(O(1)\) average- Space Complexity: \(O(N)\) to store \(N\) key-value pairs.
Summary
- Use
{}to create a dictionary. - Keys must be immutable (strings, integers, tuples).
- Use
key in dictto check for key existence in \(O(1)\) time. del dict[key]ordict.pop(key)removes elements.