Python Dictionary update()

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The update() Function is used to update the Python dictionary with the help of elements from another dictionary or an iterable object of key/value pairs. Further, in this article, we will see how this function works with the help of some examples.

Syntax of Python Dictionary update()

The update() function takes another dictionary or an iterable as a parameter.

Parameters of Python Dictionary update()

Below are the parameters of the update function :

  • other_dict or iterable : update() function takes another dictionary or an iterable containing the key-value pairs as the parameter.

Return Values of Python Dictionary update()

The update() method doesn't return anything :

  • This method just updates the existing dictionary.
  • So the return value of this method is None.

Example of Python Dictionary update()

Let's take a look at a basic example of how the Python dictionary update() function is used :

Output :

As you can see, the dictionary is updated, key-value pair is updated.

What is Python Dictionary update() ?

Suppose you are having a large dictionary containing thousands of names and the phone numbers and phone numbers of some of the people in that dictionary are to be updated. So instead of going to every key and then updating it, you can make a new dictionary or an iterable with the updated values and then pass that dictionary in the update function to update all the phone number values.

Let's take an example, if the dictionary dict1 is {'Andrew': 9999999999, 'Chandler': 8888888888} and we want to update the phone number of chandler, so for that, we can make a dictionary dict2 {'Chandler': 7777777777} and pass that dictionary dict2 in the update() function to update the values.

As the name suggests, the update() function in Python is used to update the Python dictionary with the help of elements from another dictionary or an iterable object of key/value pairs. update() method doesn't return anything.

Tip :
We can also add new keys to the dictionary using the update() function.

More Examples

Now we have learned about the update() function and now we can apply the update() function in some of our examples.

Example - 1 : update() when Tuple is Passed

We can update a dictionary by passing a list of tuples. In each tuple, the first element acts as the key for the dictionary and the second element acts as the value, and then the dictionary will be updated accordingly.

Output :

In this example, the dictionary has updated a key and then added a new key.

Example - 2 : Update with Another Dictionary

In this example, we will be passing a new dictionary to update our existing dictionary, if the key of the new dictionary exists in the older dictionary then the value of that key will be updated, else a new key will be added to the existing dictionary.

Output :

We can see the key having an existing value in the dictionary is updated and a new key is added also in the dictionary.

Example - 3 : Update with an Iterable

In this example, we will be updating the dictionary by passing the iterable values, if the key of the passed value is present in the dictionary then the value of that key will be updated, else a new key will be added to the existing dictionary. We can pass the iterable values by binding the key and values using the '=' sign.

Output :

We can see the key having an existing value in the dictionary is updated and a new key is added also in the dictionary.

Example - 4 : Python Dictionary Update Value if the Key Exists

In this example, we are checking whether the key is existing in the dictionary or not and then updating them accordingly. Let's take at the code for the following operation.

Input :

Output :

Here, a key existed and hence it was updated according to the value given in the function

Example - 5 : Python Dictionary Update Value if the Key does not exist in the Dictionary

In this example, we are checking whether the key is existing in the dictionary or not, and if it does not exist in the dictionary, then add them. Let's take at the code for the following operation.

Input :

Output :

Here, a key doesn't exist and hence it was added according to the value given in the function.

Conclusion

  • update() Function is used to update the Python dictionary with the help of elements from another dictionary or an iterable object of key/value pairs.
  • update() function takes another dictionary or an iterable as the parameter.
  • Syntax :
  • update() takes 1 parameters : other dictionary or an iterable.
  • Return Value is None.
  • We have discussed the following approaches for using the update() function :
    • Update when Tuple is Passed
    • Update with another Dictionary
    • Update with an iterable
    • Python dictionary update value if the key exists
    • Python dictionary update value if the key does not exist in the dictionary

See Also