Python Dictionary update() Method

The update() method in Python is a built-in method that is used to update the key-value pairs in a dictionary. This method allows you to update the values of existing keys, or add new key-value pairs to the dictionary. It takes a dictionary, or an iterable of key-value pairs, as its argument, and merges the key-value pairs of the argument into the dictionary on which the method is called.

Syntax

dict.update(other_dict)

Parameters

The update() method takes the following parameters:

  • other_dict: The dictionary or iterable of key-value pairs to merge into the calling dictionary. This parameter is required.

Examples

Example 1: Updating the Values of Existing Keys

Consider the following example, where we update the values of existing keys in a dictionary:

student = {'name': 'John', 'age': 25}
new_student_data = {'age': 26, 'gender': 'Male'}
student.update(new_student_data)
print(student)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then, we have created another dictionary called new_student_data, which contains new data for the student. Finally, we use the update() method to merge the key-value pairs of the new_student_data dictionary into the student dictionary. The final dictionary, which we print, will be:

{'name': 'John', 'age': 26, 'gender': 'Male'}

As you can see, the values of the keys ‘age’ and ‘gender’ have been updated in the student dictionary, based on the values in the new_student_data dictionary.

Example 2: Adding New Key-Value Pairs to the Dictionary

Consider the following example, where we add new key-value pairs to a dictionary:

student = {'name': 'John', 'age': 25}
new_student_data = {'gender': 'Male', 'address': '123 Main St'}
student.update(new_student_data)
print(student)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then, we have created another dictionary called new_student_data, which contains new data for the student. Finally, we use the update() method to merge the key-value pairs of the new_student_data dictionary into the student dictionary. The final dictionary, which we print, will be:

{'name': 'John', 'age': 25, 'gender': 'Male', 'address': '123 Main St'}

As you can see, the new_student_data dictionary has been merged into the student dictionary, and the key-value pairs for ‘gender’ and ‘address’ have been added to the student dictionary.

Example 3: Updating Values with a Dictionary and a Key-Value Pair

Consider the following example, where we update the values in a dictionary using both a dictionary and a key-value pair:

student = {'name': 'John', 'age': 25}
new_student_data = {'age': 26, 'gender': 'Male'}
student.update(new_student_data, address='123 Main St')
print(student)

In this example, we have created a dictionary called student, which contains the key-value pairs of a student. Then, we have created another dictionary called new_student_data, which contains new data for the student. Finally, we use the update() method to merge the key-value pairs of the new_student_data dictionary and the key-value pair of ‘address’ into the student dictionary. The final dictionary, which we print, will be:

{'name': 'John', 'age': 26, 'gender': 'Male', 'address': '123 Main St'}

As you can see, both the new_student_data dictionary and the key-value pair of ‘address’ have been merged into the student dictionary, and the values of the keys ‘age’ and ‘gender’ have been updated based on the values in the new_student_data dictionary.

In conclusion, the update() method is a very useful method for updating the key-value pairs in a dictionary, or adding new key-value pairs to a dictionary. By using this method, you can easily merge the contents of one dictionary into another dictionary, or update the values of existing keys in a dictionary. It is a must-know method for anyone working with dictionaries in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *