Hacker News new | past | comments | ask | show | jobs | submit login

Why not use `update` in Python? e.g. if you want to merge dictA and dictB, you'd write

    dictA.update(dictB)
If you have many you can always use this:

    dictD = {}
    map(dictD.update, [dictA, dictB, dictC])



Or

    dictAB = dict(dictA, **dictB)
in Python 2 and 3.


That'll create an intermediate dict (due to the unpacking) for Python <3.6 and then another dict. a.update(b) does neither.

It also has different semantics from a.update(b). The unpacking way will explode when both a and b contain the same key, while a.update(b) does not; existing keys also contained in b will be overwritten by the values from b.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: