What is update() method in dictionary | How to use the update() method of the dictionary.

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()_method_list_dictionary_code

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.

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.