Dict Syntax

Published March 19, 2012 · 1 Minute Read · ∞ Permalink


I just found out something neat about Python’s dict syntax. First of all, of course this works:

>>> d = dict(a=1, b=2, c=3)

But you can also stick dictionaries in there as the first argument. Go ahead, try it:

>>> d = {'a': 1, 'b': 2}
>>> d2 = dict(d, a=2, b=3)
>>> d2
{'a': 2, 'b': 3}

It’s as if you called update on the dictionary. Neato, eh? It’s especially useful in ming migrations.