setdefault()

Using the setdefault() method you can check if a key exists and if it doesn’t set it to a default value.

@>>> spam = {'name': 'Pooka', 'age': 5}
@>>> spam.setdefault('color', 'black')
'black'
@>>> spam
{'name': 'Pooka', 'age': 5, 'color': 'black'}
@>>> spam.setdefault('color', 'white')
'black'
@>>> spam
{'name': 'Pooka', 'age': 5, 'color': 'black'}

Note, can’t set color after it has already been set.