The update method in Python dictionaries is a built-in function that allows you to modify, add, or merge key-value pairs. This article will cover the syntax, parameters, return values, and examples of using the update method.
-
Syntax of the update method The syntax for the update method is as follows: dict.update(dict2) Here, 'dict' is the dictionary to be updated and 'dict2' can be another dictionary or an iterable sequence of key-value pairs to add to 'dict'.
-
Parameters of the update method The parameters for the update method can include:
- Another dictionary: In this case, all key-value pairs from dict2 are added to dict; if keys already exist in dict, their values will be overwritten by those from dict2.
- An iterable sequence of key-value pairs: Each element in this sequence (which could be a list, tuple, set etc.) must consist of two items representing a key and its corresponding value. Existing keys in dict will have their values replaced by those from this sequence.
- A keyword argument list: Here each parameter provided as a keyword argument during function call (e.g., name='Alice') becomes a key-value pair added to dict; existing keys will also get overwritten if they match.
-
Return Value of the update Method The update method does not return any value; it modifies the original dictionary directly without creating a new one. After calling it, you can simply use 'dict' to see your updates reflected.
-
Purpose of the Update Method The primary purposes are:
- To modify existing entries within a dictionary: If both dictionaries share common keys, values from dict2 replace those in dict.
- To merge two dictionaries: New entries from dict2 are added into dict when there are no matching keys present between them.
- Examples Using Update Method Below are some examples demonstrating how to use
updatefor updating or merging dictionaries: 5.1 Updating with another dictionary object as parameter
Create two dictionaries
person1 = {'name': 'Alice', 'age': 20, 'gender': 'female'}
person2 = {'name': 'Bob', 'age': 21,'gender':'male','hobby':'basketball'}
Use person2 to update person1
person1.update(person2)
printf(person1) # Output would show updated person1 contents
outcome: {'name': 'Bob', ‘age’: 21,’gender’:’male’,’hobby’:’basketball’}
here we see that name age gender were replaced while hobby was newly added into person1’s structure .
5.2 Updating with an iterable sequence as parameter
here's another example where we create student info :
student = {'name':'Tom','age':18,'score':90}
lst_info=[('class','A'),('rank',3),('score',95)] # List containing tuples acting like Key/Value Pairs student.update(lst_info) #Update student data based on lst_info content print(student)
could yield result :{'name':'Tom','age ':18,'score ':95 ,'class ':'A ','rank ':3 }
in which score got overridden but class & rank were introduced .
basically similar approach applies across different scenarios too! For instance,
suppose car details need adjustments ; here’s how we’d do it!
a vehicle_dict={'brand':'BMW','color':'black','price ':500000}
v_vehicle_dict.update(color='red ',year=2020 )
to check results print(vehicle_dict); expected output being {‘brand ’:'BMW’,‘color ’:'red’,‘price ’:500000 ,‘year ’ :2020 }.
