Daily Software Tips & Tricks
Bite-sized knowledge to improve your coding skills daily.
Safeguard your Dictionary lookups
March 24, 2026
Accessing keys in a Python dictionary using the square bracket notation like data['key'] is fine until the key is missing and your app crashes with a KeyError. A much cleaner approach is using the .get() method. It allows you to provide a default value as the second argument, such as data.get('status', 'unknown'). This keeps your code robust and prevents unnecessary try-except blocks for simple lookups.