intersection, union, difference, symmetric difference for dictionaries

mauro mauro at gmail.com
Tue Feb 25 23:11:07 CET 2014


> 
> {1, 2} & {2, 3} == {2}
> 
In my mind the intersection is evaluated on keys, so the resulting dict 
should be the empty one
> but
> 
> {1:"a", 2:"b", 3:"c"} & {2:"b", 3:"e", 4:"f"} == ???
my output will be
{2:"b", 3:"e"} 
or
{2:"b", 3:"c"} 

depending on the implementation choice.  
> 
> The most obvious result is probably the empty dict {2:"b"}, i. e.
> 
> a & b is defined as dict(a.items() & b.items())
> 
> Frankly, I don't do that a lot. So what's your use-case?
I do not have an use case, but I've seen that many people ask for these 
operations for example in stackoverflow.com




More information about the Python-list mailing list