Complete Guide to Python Dictionary
Initialization
d = {
"key1": "value1",
"key2": "value2"
}
Access
- The in-operator can be used to check a key exists in a dictionary.
print("key3" in d) # output: False
- The subscript-operator can be used for accessing existing keys. KeyError-exception will be thrown for non-existing keys.
print(d["key1"]) # output: value1
- The get-method can also be used to access values with support for a default value. The default value is for non-existing keys.
print(d.get("key3", "default-value")) # output: default-value
- d.keys() : The list of keys, in arbitrary order.
print(d.keys()) # output: dict_keys(['key1', 'key2'])
- d.values() : The list of values.
print(d.values()) # output: dict_values(['value1', 'value2'])
- d.items(): The list of (key, value) tuple pairs.
for k, v in d.items():
print(k, v)
Update
- Use the subscript-operator for updating any value or adding new value.
d["newkey"] = "newkeyValue"
d["exisitngKey"] = "newValue"
- d.update(anotherDict): Adds anotherDict’s key-values pairs to d.
d = { "k1": "v1", "k2": "v2" }
anotherDict = { "k2": "nv2", "k3": "nv3" }
d.update(anotherDict)
print(d) # output: {'k1': 'v1', 'k2': 'nv2', 'k3': 'nv3'}
Interview Questions
1. What are characteristics of the dictionary key?
- keys can be any immutable type; strings and numbers can always be keys.
- Tuples can be used as keys if they contain only immutable items like strings, numbers, etc. If a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
- You can’t use a list as a key, since lists are mutable.
2. What is the difference between using subscript-operator and the get-method in a dictionary?
Both can be used to access the value of a key. However, subscript operator will throw KeyError if the key doesn’t exist whereas get-method will return None if the key doesn’t exist. Moreover, the second argument to get-method is the default value if the key doesn’t exist.