Dictionary¶
Snippets on dict manipulation.
merge (union) two Python dictionaries¶
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = x.copy()
>>> z.update(y)
{'a': 1, 'c': 11, 'b': 10}
create a dictionary with list comprehension¶
In Python 2.6 (or earlier), use the dict constructor:
d = dict((key, value) for (key, value) in sequence)
In Python 2.7+ or 3, you can just use the dict comprehension syntax directly:
d = {key: value for (key, value) in sequence}