Dictionary Cheatsheet
Complete Python Dictionary cheatsheet with clear examples for quick revision
Example: my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
2
4
7
8
9
10
setdefault(key, value)
Returns the value for a key if it exists. If not, inserts the key with a value of default and returns default.
my_dict.setdefault('age', 30) # {'name': 'John', 'age': 25, 'city': 'New York'}
my_dict.setdefault('height', 175) # {'name': 'John', 'age': 25, 'city': 'New York', 'height', 175}