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:
Code In Text Form.
dictionary = {1: ‘Student’,
‘Name’: ‘Waqar Khan’,
‘Subjects’: ‘Computers’}
dictionary.update({‘University’:’MU’})
print(dictionary)
Code:
- A dictionary with some random values is being taken
- update() method is being applied and a single value dictionary is being passed as argument.
Output:
- 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.
Update() Method In Dictionary Update() Method In Dictionary In Python What Is Update() Method In Dictionary In Python
Source link
Leave a Reply