Get dictionary key with default if not found using method get()
This is better approach in most cases.
d = {"k1" : "v1"}
print d.get("k2", "Default")
print d.get("k2")Default None
Env: Python 2.7.18
Get dictionary key using square brackets ([])
This will raise KeyError exception if key is not present.
d = {"k1" : "v1"}
print d["k2"]Traceback (most recent call last):
File "test2.py", line 2, in <module>
print d["k2"]
KeyError: 'k2'
Env: Python 2.7.18