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 |
dict1 | The dictionary to be updated. |
dict2 | The source dictionary for the update, where new key/value pairs will be drawn from. |
|
Related |
Dictionary
|
---|
Updated on Sun Nov 26 15:34:43 2017.