- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update the value of a key in a dictionary in Python?
Python dictionary is an unordered collection of items. Each item of a dictionary has a key/value pair. And these are indexed by keys not with the index positions like other data types.
To update the value of a key in a dictionary we can simply update the value just by referencing the key directly, or we can use one of the dictionary method update() also.
In both ways, If an existing key is used in the expression, its associated value will be updated, otherwise a new key−value pair will be added in the dictionary object. Let’s learn about the update() method first.
Syntax
dict.update([other])
Parameters
The update method takes either a dictionary or an iterable object of key/value pair as parameters.
Return Value
The method doesn’t return any output, instead it updates the Dictionary with newly associated values of the keys.
Using the update() method
Let’s take an example and see how the dict.update() method updates the value of a dictionary element based on key reference.
Example
d = {'A': 1, 'B': 5, 'C': 2} print("Original Dictionary: ", d) d.update({'A': 100}) print("Updated Dictionary: ",d)
Output
Original Dictionary: {'A': 1, 'B': 5, 'C': 2} Updated Dictionary: {'A': 100, 'B': 5, 'C': 2}
The value 1 referring to the key “A” is changed to 100, here we specified {‘A’: 100} to the update method. Take another example and see what will happen if the reference key is not present in the dictionary object.
Example
We assigned an unused reference key to the update() method, so it has created the new key/value pair with assigned ones.
d = {'A': 1, 'B': 5, 'C': 2} print("Original Dictionary: ", d) d.update({'D': 10}) print("Updated Dictionary: ",d)
Output
Original Dictionary: {'A': 1, 'B': 5, 'C': 2} Updated Dictionary: {'A': 1, 'B': 5, 'C': 2, 'D': 10}
Example
d = {'one':1, 'two':2,'three':3,'four':4} print("Original Dictionary: ", d) d.update({'one': '111', 'ten': 10}) print("Updated Dictionary: ",d)
Output
Original Dictionary: {'one': 1, 'two': 2, 'three': 3, 'four': 4} Updated Dictionary: {'one': '111', 'two': 2, 'three': 3, 'four': 4, 'ten': 10}
In this above example, we have successfully updated the 2 key:value pairs using dict.update() method.
By directly assigning values
Without using any method we can simply assign dict[key] = new_value to update a value like the previous examples. Let’s take an example and see how to change the value of an existing key without using the method.
Example
d = {'one':1, 'two':2,'three':3,'four':4} print("Original Dictionary: ", d) d['one'] = '111' print("Updated Dictionary: ",d)
Output
Original Dictionary: {'one': 1, 'two': 2, 'three': 3, 'four': 4} Updated Dictionary: {'one': '111', 'two': 2, 'three': 3, 'four': 4}
The value referring key “one” is updated with a new value “111” successfully.