Skip to content

Python update dictionary value | Example code

  • by

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}

marks.update(internal_marks)

print(marks)

Output:

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})

print(dict)

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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Your email address will not be published. Required fields are marked *