You can add new keys to a dictionary in Python by assigning a value to a new key using the square bracket notation. Let’s see some examples.
In this example, an empty dictionary my_dict is created, and then new keys 'key1', 'key2', and 'key3' are added along with their respective values.
The output will be:
{'key1': 'value1', 'key2': 42, 'key3': [1, 2, 3]}
However, you can use the update() method to add multiple key-value pairs.
The output:
{'key1': 'value1', 'key2': 42, 'key3': [1, 2, 3], 'key4': 'value4', 'key5': 3.14}
Continue your learning with more such Python blogs from MarsDevs!