How to update a dictionary in Python

Updating a dictionary inserts a new entry into the dicitionary or modifies an existing one. For example, updating {“a”: 1, “b”: 2} with {“c”: 3} returns {“a”: 1, “b”: 2, “c”: 3}.

Solution for How to update a dictionary in Python : You can use dict.update() to update a dictionary Call dict.update(other_dictionary) to update dict with key-value pairs from other_dictionary. If a key in other_dictionary already exists in dict, its value is replaced. If not, the key-value pair is added to dict.

Alternatively, call dict.update(iterable) with iterable as a sequence of key = value assignments.

a_dictionary = {"a": 1, "b": 2}


how-to-update-a-dictionary-in-python