Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!Using the Operator
The "[]" operator (used to access value mapped to a dictionary key) is used to update an existing key-value pair as well as add a new pair.
Syntax
If the key is already present in the dictionary object, its value will be updated to val. If the key is not present in the dictionary, a new key-value pair will be added.
Example
In this example, the marks of "Laxman" are updated to 95.
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 95, 'David': 49}
Example
However, an item with 'Krishnan' as its key is not available in the dictionary, hence a new key-value pair is added.
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49, 'Krishan': 74}
Using the update() Method
You can use the update() method in dict class in three different ways:
Update with Another Dictionary
In this case, the update() method's argument is another dictionary. Value of keys common in both dictionaries is updated. For new keys, key-value pair is added in the existing dictionary
Syntax
Return value
The existing dictionary is updated with new key-value pairs added to it.
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Update with Iterable
If the argument to update() method is a list or tuple of two item tuples, an item each for it is added in the existing dictionary, or updated if the key is existing.
Syntax
Return value
Existing dictionary is updated with new keys added.
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Update with Keyword Arguments
Third version of update() method accepts list of keyword arguments in name=value format. New k-v pairs are added, or value of existing key is updated.
Syntax
Return value
Existing dictionary is updated with new key-value pairs added.
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Using the Unpack Operator
The "**" symbol prefixed to a dictionary object unpacks it to a list of tuples, each tuple with key and value. Two dict objects are unpacked and merged together and obtain a new dictionary.
Syntax
Return value
Two dictionaries are merged and a new object is returned.
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Using the Union Operator (|)
Python introduces the "|" (pipe symbol) as the union operator for dictionary operands. It updates existing keys in dict object on left, and adds new key-value pairs to return a new dict object.
Syntax
Return value
The Union operator return a new dict object after merging the two dict operands
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}
Using "|=" Operator
The "|=" operator is an augmented Union operator. It performs in-place update o n the dictionary operand on left by adding new keys in the operand on right, and updating the existing keys.
Syntax
Example
It will produce the following output −
marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49} marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.