What is usage of update method in Python dictionary?

PythonServer Side ProgrammingProgramming

The update() method of Python’s dictionary class serves two purposes.

It adds a new key-value pair if dictionary doesn’t already contain the key.

>>> d1={'name': 'Ravi', 'age': 25, 'marks': 60}

The update() method a dictionary object as argument


The updated dictionary shows that a new key-value pair is added

>>> d1
{'name': 'Ravi', 'age': 25, 'marks': 60, 'course': 'Computer Engg'}

However, if the key as already present, its value is updated by the method

>>> d1
{'name': 'Ravi', 'age': 21, 'marks': 60, 'course': 'Computer Engg'}

Published on 09-Feb-2018 00:18:25