Setdefault
Trying to access a key in a dictionary that does not exist will result a KeyError exception.
Using the get
method we can avoid this. The get
method, will return the value of the key if the key exists. None if the key does not exists, or a default value if it was supplied to the get
method.
This will not change the dictionary.
Using the setdefault
method is similar to the get
method but it will also create the key with the given value.
grades = {}
# print(grades['python']) # KeyError: 'python'
print(grades.get('python')) # None
print(grades.get('python', 'snake')) # snake
print(grades) # {}
print(grades.setdefault('perl')) # None
print(grades) # {'perl': None}
print(grades.setdefault('python', 'snake')) # 'snake'
print(grades) # {'perl': None, 'python': 'snake'}
print(grades.setdefault('python', 'boa')) # 'snake'
print(grades) # {'perl': None, 'python': 'snake'}