Python Tutorial Python Advanced Python References Python Libraries

Python Dictionary - update() Method



The Python update() method is used to insert specified key-value pair(s) in the dictionary.

Syntax

dictionary.update(iterable)

Parameters

iterable Required. a dictionary or other iterable with key-value pair(s)

Return Value

None.

Example: Update with Dictionary

In the example below, the update() method is used to update the given dictionary using another dictionary.

MyDict = {
  'name': 'John'
}
MoreInfo = {'age': 25, 'city': 'London'}
MyDict.update(MoreInfo)      
print(MyDict)

The output of the above code will be:

{'name': 'John', 'age': 25, 'city': 'London'}

Example: Update with Other Iterables

In the example below, the dictionary is updated using iterables.

MyDict = {
  'name': 'John'
}
MyDict.update(age = 25, city = 'London')      
print(MyDict)

The output of the above code will be:

{'name': 'John', 'age': 25, 'city': 'London'}

❮ Python Dictionary Methods