Python update dictionary value | Example code

Use update() method to update dictionary value in Python. This method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.

So Use the existing key if you want to update the value for a particular key in the dictionary.

Example update dictionary value in Python

Simple example code.

marks = {'Physics': 100, 'Maths': 50}

internal_marks = {'Maths': 75}




Python update dictionary value

How to update the value of a key in a dictionary in Python?

Answer: Use the update method with key-value pair to update the value of the given key. Kye must exist in the dictionary When passing a key-value pair in the update method. Otherwise, the new value will be appended in the dictionary.

dict = {'Africa': 200, 'Australia': 300, 'England': 400}

dict.update({'Africa': 500})


Output: {‘Africa’: 500, ‘Australia’: 300, ‘England’: 400}

Do comment if you have any doubts and suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.