On 2016-11-11 13:29, Peter Otten wrote: > The same using update(), with a generator expression that avoids > the intermediate dict: > > >>> dict1 = {'A': 'a', 'B': 'b', 'C': 'c'} > >>> dict1.update((k, dict2[k]) for k in desired & dict1.keys() & > dict2.keys())
Huh. Handy to file that new knowledge away. I'd not realized it could take a 2-tuple iterable, but my previous example would then be more cleanly written as dict1.update((k,v) for k,v in dict.items() if k in desired) But yes, certainly a couple edge cases depending on the dict sizes, the size of the "desired" set, and what should happen in the event dict2 (or "desired") has keys that dict1 doesn't. -tkc -- https://mail.python.org/mailman/listinfo/python-list