Last Updated: February 25, 2016
·
486
· eallik

Merge dict (e.g. in a listcomp/genexp)

Before I was doing:

[dict(x.items() + {'added_key': 123}.items()) for x in some_dicts]

...but thanks to http://stackoverflow.com/questions/1452995/why-doesnt-a-python-dict-update-return-the-object (which, btw, is otherwise also very educating), am now doing:

[dict(x, added_key=123) for x in some_dicts]

—really nice.

P.S. for functools lovers: map(partial(dict, added_key=123), some_dicts)