intersection, union, difference, symmetric difference for dictionaries

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 25 23:21:05 CET 2014


Tim Chase <python.list at tim.thechases.com> wrote:

>   a_dict = dict(...)
>   b_dict = dict(...)
>   a_set = set(a_dict)
>   b_set = set(b_dict)
>   added_keys = b_set - a_set
>   removed_keys = a_set - b_set
>   same_keys = a_set & b_set
>   diff_keys = a_set ^ b_set
>   all_keys = a_set | b_set
> 
> It would save some space if I didn't have to duplicate all the keys
> into sets (on the order of 10-100k small strings), instead being able
> to directly perform the set-ops on the dicts.  But otherwise, it was
> pretty readable & straight-forward.
> 
It doesn't matter whether they were small strings or full-length novels, 
creating a set from a dict doesn't duplicate any strings.


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list