Python dictionary provides a member function update() function. Let’s use this to add new key value pairs in dictionary or updating existing one. Below is an example to add new keys to a dictionary in Python.
1 2 3 4 5 6 | d = {'key': 'value'} print(d) # {'key': 'value'} d['mynewkey'] = 'mynewvalue' print(d) # {'key': 'value', 'mynewkey': 'mynewvalue'} |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.