In this Python tutorial, you will learn what update() method of dictionary dict class does, its syntax, and how to use this method to access all the keys in the dictionary, with example programs.

Python Dictionary update()

Python Dictionary update() method updates this dictionary with the key:value pairs from the other dictionary.

If this dictionary has keys same as that of in other dictionary, the values in this dictionary are updated with values from other dictionary.

If other dictionary has keys which this dictionary has not, those key:value pairs are added to this dictionary.

Syntax

The syntax of dict.update() is

</>
Copy
dict.update([other])

where

  • other is a dictionary from which this dictionary gets updated.

Examples (4)

1. Update values of a dictionary with the values from another dictionary

In this example, we will take dictionary with some key:value pairs and update this dictionary with other dictionary.

The other dictionary has key:value pairs such that it contains only some of the keys present in original dictionary.

Python Program

</>
Copy
myDictionary = {'a': 58, 'b': 61, 'c': 39}
other = {'b': 44, 'c': 22}
myDictionary.update(other)
print(myDictionary)

Program Output

{'a': 58, 'b': 44, 'c': 22}

In other dictionary, we have keys 'b‘ and 'c'. The values of these keys are updated in the original dictionary.

2. Insert new keys in this dictionary that are present in other dictionary

In this example, we will take other dictionary, such that it contains new keys which are not present in the original dictionary.

Python Program

</>
Copy
myDictionary = {'a': 58, 'b': 61, 'c': 39}
other = {'k': 44, 'm': 22}
myDictionary.update(other)
print(myDictionary)

Program Output

{'a': 58, 'b': 61, 'c': 39, 'k': 44, 'm': 22}

other dictionary has keys 'k' and 'm' which are not present in the original dictionary. So, these key:value pairs are added to the original dictionary.

3. Update/insert entries in this dictionary by looking at other dictionary

In this example, let us have a mix of the above two scenarios. Other dictionary has keys such that some match with that of in this dictionary and some does not.

Python Program

</>
Copy
myDictionary = {'a': 58, 'b': 61, 'c': 39}
other = {'b': 44, 'm': 22}
myDictionary.update(other)
print(myDictionary)

Program Output

{'a': 58, 'b': 44, 'c': 39, 'm': 22}

The keys that are already present are updated in the original dictionary with new value from other dictionary. And the keys that are not present are added to the original dictionary.

4. No argument passed to the dict.update() method

In this example, we will call update() method with no other dictionary passed as argument.

Python Program

</>
Copy
myDictionary = {'a': 58, 'b': 61, 'c': 39}
myDictionary.update()
print(myDictionary)

Program Output

{'a': 58, 'b': 61, 'c': 39}

As there is nothing to update from, the original dictionary remains unchanged.

Conclusion

In this Python Tutorial, we learned about Python Dictionary method dict.update().