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)
Written by Erik Allik
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#