What is usage of update method in Python dictionary?


The update method is one of the methods of 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.

The dictionary is represented by curly braces{}.The dictionary contains key and value pairs, combinedly called as 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 colon and the key and values in the dictionary combinedly 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 values then along with the key use indexing.

Syntax

The following is the syntax for using the update method of the dictionary.

d_name.update({k1:v1})

Where,

  • d_name is the name of the dictionary

  • update is the name method

  • k1 is the key

  • v1 is the value

Example

When we pass the key value pairs in the update function then those defined key and value 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)

Output

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

This is another example of using the update function for updating the items to 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)

Output

The created dictionary: {'a': 10, 'b': 20, 'c': 30}
The updated dictionary: {'a': 10, 'b': 20, 'c': 25}

Example

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)

Output

The created dictionary: {'a': 10, 'b': 20, 'c': 30}
The updated dictionary: {'a': 10, 'b': 20, 'c': 25, 'd': 40}

Example

This is another example to understand the update method for updating the multiple items to 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)

Output

The created dictionary: {'a': 10, 'b': 20, 'c': 30}
The updated dictionary: {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50, 'f': 50}

Updated on: 15-May-2023

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements