What Is Update() Method In Dictionary In Python-Kaizensk

Definition:

The update() method inserts the specified item into the existing dictionary. The item must be specified into curly braces meaning in dictionary format. This method changes the original dictionary.

Syntax:

Dictionary.update({key : value})

This method takes dictionary or iterable object as argument

Working:

Update()_list_dictionary_method_output

Code In Text Form.

dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Subjects’: ‘Computers’}
dictionary.update({‘University’:’MU’})
print(dictionary)

Code:

  1. A dictionary with some random values is being taken
  2. update() method is being applied and a single value dictionary is being passed as argument.

Output:

  1. An updated dictionary with new items is printed.

Interview questions:

  • Can we add two dictionaries?

Yes we can do so by using update() method we just have to pass the other dictionary as an argument to the first dictionary.



Source link