How to Use Dictionary update() Method in PythonPython Basics


The update() method updates the dictionary with the elements from either dictionary object or an iterable of key-value pairs.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict1 = {'Name': 'Volkswagen', 'Vehicle type': 'SUV'}
dict2 = {'Turbo':'no','Price': 150000,'gearbox': 'automatic-shift'}
print(dict1)
print(dict2)

dict1.update(dict2)
print(dict1)
print(dict2)

Output:

{'Name': 'Volkswagen', 'Vehicle type': 'SUV'}
{'Turbo': 'no', 'Price': 150000, 'gearbox': 'automatic-shift'}
{'Name': 'Volkswagen', 'Vehicle type': 'SUV', 'Turbo': 'no', 'Price': 150000, 'gearbox': 'automatic-shift'}
{'Turbo': 'no', 'Price': 150000, 'gearbox': 'automatic-shift'}

Syntax

dict1.update(dict2)

Parameters

dict1 and dict2 are two dictionaries.

Return Value

It doesn’t any values.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us