How to Update a Python Dictionary?

Python Update Dictionary

Hey, folks! In this article, we will be unveiling the process to Update a Python Dictionary.


Getting started with the Steps to Update a Python Dictionary

Python Dictionary is a data structure that holds the data elements in a key-value pair and basically serves as an unordered collection of elements. In order to update the value of an associated key, Python Dict has in-built method — dict.update() method to update a Python Dictionary.

The dict.update() method is used to update a value associated with a key in the input dictionary.

Syntax:

input_dict.update(dict)

The function does not return any values, rater it updates the same input dictionary with the newly associated values of the keys.

Example:

dict = {"Python":100,"Java":150}
up_dict = {"Python":500}
print("Dictionary before updation:",dict)
dict.update(up_dict)
print("Dictionary after updation:",dict)

Output:

Dictionary before updation: {'Python': 100, 'Java': 150}
Dictionary after updation: {'Python': 500, 'Java': 150}

Update a Python Dictionary with an Iterable

Apart from updating the key-values of the dictionary, we can append and update a Python Dictionary with values from other iterables as well.

Syntax:

dict.update(iterable)

Example:

dict = {"Python":100,"Java":150}
print("Dictionary before updation:",dict)
dict.update(C = 35,Fortran = 40)
print("Dictionary after updation:",dict)

In the above example, we have updated the input dict with the values passed to the update() function. Thus, the input dict gets appended and updated with the values passed to the function.

Output:

Dictionary before updation: {'Python': 100, 'Java': 150}
Dictionary after updation: {'Python': 100, 'Java': 150, 'C': 35, 'Fortran': 40}

Updating Nested Python Dictionary

A Nested Dictionary is a dictionary within a dictionary. Python Nested dictionaries can be updated with the respective key values using the following syntax:

Syntax:

dict[outer-key][inner-key]='new-value'

Example:

dict = { 'stud1_info':{'name':'Safa','Roll-num':25},'stud2_info':{'name':'Ayush','Roll-num':24}}
print("Dictionary before updation:",dict)
dict['stud2_info']['Roll-num']=78
dict['stud1_info']['name']='Riya'
print("Dictionary after updation:",dict)

In the above example, we have updated the value of inner key:’Roll-num’ of the outer key:’stud2_info’ to 78 and value of inner key:’name’ of the outer key:’stud1_info’ to ‘Riya’.

Output:

Dictionary before updation: {'stud1_info': {'name': 'Safa', 'Roll-num': 25}, 'stud2_info': {'name': 'Ayush', 'Roll-num': 24}}
Dictionary after updation: {'stud1_info': {'name': 'Riya', 'Roll-num': 25}, 'stud2_info': {'name': 'Ayush', 'Roll-num': 78}}

Conclusion

Thus, in this article, we have understood the way to update values to a Python Dictionary as well as a Nested Dictionary.

I would strongly recommend the readers to go through the Python Dictionary Tutorials to have a deep understanding about the Dictionary concepts.


References

  • Python Dictionary — JournalDev