How to add keys to Python dictionary

Default featured post

Python has a strong feature to allow you to check availability of keys in a dictionary (AKA JSON string) on the fly and edit it. I recently, ran to a case that requires to check whether a particular keys are available in the dictionary. If keys are unavailable, add to the dictionary and persist it to the storage.

Since I am new to Python world, I did a bit of Google search and came up with a following solution.
First step is to check whether the keys exist in the dictionary/JSON string. To do that, I use this code,

if Parameter.KEY1 not in self.json_configuration:

Then need to add the key inside of the if block, like this:

self.json_configuration[Parameter.KEY1] = 'False'

As you can see, adding a key does not differ from variables manipulation at all and that’s the strength of Python.
The last step is to persist the changes to the disk, this step is also easy peasy.

with open(self.file_name, 'w') as f:
json.dump(content, f)