Name | .update() |
||||
---|---|---|---|---|---|
Examples |
element_names = {'H': 'hydrogen', 'He': 'helium', 'Li': 'lithium'} additional_elements = {'B': 'boron', 'C': 'carbon', 'N': 'nitrogen'} print(len(element_names)) # Prints 3 # "Update" original dictionary with new values element_names.update(additional_elements) print(len(element_names)) # Prints 6 print(element_names['N']) # Prints "Nitrogen" # Updating with an existing key will overwrite the value for that key! wrong_elements_oh_no = {'H': 'Happiness'} element_names.update(wrong_elements_oh_no) print(element_names['H']) # Prints "Happiness" | ||||
Description | "Updates" a dictionary with the contents of a second dictionary. Any key/value pairs present in the second dictionary will be added to the first. If the second dictionary contains a key present in the first dictionary, its value will be overwritten with the value from the second dictionary. | ||||
Syntax | dict1.update(dict2) | ||||
Parameters |
| ||||
Related |
Dictionary |
Updated on Tue Feb 27 14:07:12 2024.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License