Updating dictionaries

If you need to extend a dictionary with definitions from another dictionary you can use the update() method. This is called on the dictionary to be updated and is passed the contents of the dictionary which is to be merged in:

>>> g = dict(wheat=0xF5DEB3, khaki=0xF0E68C, crimson=0xDC143C)
>>> f.update(g)
>>> f
>>> {'crimson': 14423100, 'indigo': 4915330, 'goldenrod': 14329120,
'wheat': 16113331, 'khaki': 15787660, 'seashell': 16774638}

If the argument to update() includes keys which are already present in the target dictionary, the values associated with these keys are replaced in the target by the corresponding values from the source:

>>> stocks = {'GOOG': 891, 'AAPL': 416, 'IBM': 194}
>>> stocks.update({'GOOG': 894, 'YHOO': 25})
>>> stocks
{'YHOO': 25, 'AAPL': 416, 'IBM': 194, 'GOOG': 894}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset