
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Usage of update() Method in Python Dictionary
The update method is one of the methods of the dictionary data structure. It is used to update the values in the already created dictionary, which means it adds a new key-value pair to the dictionary. The updated key and value will be updated at the last.
Python update() Method in Dictionary
The dictionary is represented by curly braces{}. The dictionary contains key and value pairs, collectively called items, which can accept any data type of elements as values. It is mutable, which means once the dictionary is created, we can apply the changes.
It has key-value pairs separated by colons, and the key and values in the dictionary are collectively called an item. The key is unique, whereas values can have duplicates. Indexing won't work for dictionaries; if we want to access a value, we have to use a key, and when we want to access a particular key value, then, along with the key, use indexing.
Syntax of 'update()' Method
The following is the syntax for using the update method of the dictionary.
d_name.update({k1:v1})
Example 1
When we pass the key-value pairs in the update function, then those defined keys and values will be updated in the dictionary.
We created a dictionary with keys and values, assigning the result to variable d1. We then printed this dictionary by calling its name, d1. Following that, we updated d1 with a new key-value pair using the update method. Finally, we printed the resulting dictionary so as to compare it to the original version.
d1 = {"a":10,"b":20,"c":30} print("The created dictionary:",d1) d1.update({"d":40}) print("The updated dictionary:",d1)
The following is the output of the update method of the dictionary. We can observe the items updated at the end of the dictionary.
The created dictionary: {'a': 10, 'b': 20, 'c': 30} The updated dictionary: {'a': 10, 'b': 20, 'c': 30, 'd': 40}
Example 2
This is another example of using the update function for updating the items in the dictionary. The following is the code -
d1 = {"a":10,"b":20,"c":30} print("The created dictionary:",d1) d1.update({"c":25}) print("The updated dictionary:",d1)
Following is the output of the above program -
The created dictionary: {'a': 10, 'b': 20, 'c': 30} The updated dictionary: {'a': 10, 'b': 20, 'c': 25}
Example 3
In this example, when we pass multiple items to the dictionary, then the dictionary is updated with those items.
d1 = {"a":10,"b":20,"c":30} print("The created dictionary:",d1) d1.update({"c":25,"d":40}) print("The updated dictionary:",d1)
Following is the output of the above code -
The created dictionary: {'a': 10, 'b': 20, 'c': 30} The updated dictionary: {'a': 10, 'b': 20, 'c': 25, 'd': 40}
Example 4
This is another example to understand the update method for updating multiple items in the dictionary. The following is the code.
d1 = {"a":10,"b":20,"c":30} print("The created dictionary:",d1) d1.update({"d":40,"e":50,"f":50}) print("The updated dictionary:",d1)
Following is the output of the above code -
The created dictionary: {'a': 10, 'b': 20, 'c': 30} The updated dictionary: {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50, 'f': 50}