intersection, union, difference, symmetric difference for dictionaries

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 25 19:37:44 EST 2014


On Tue, 25 Feb 2014 23:07:28 +0000, MRAB wrote:

> On 2014-02-25 21:27, Ben Finney wrote:

>> Or rather, sets *only* have values. Dictionaries have keys, sets do not
>> have keys.
>>
> But a dictionary can have duplicate values, a set cannot.

It is usual to talk about the things stored in dicts and sets using 
slightly different terminology:

  - mappings such as dicts have *key/value pairs*

  - sets have *elements*

The elements of a set are most closely related to the keys of a mapping, 
not the values. Set elements are unique, just like mapping keys. 
Specifically, the keys in a mapping make up a set; the values in a 
mapping do not.

When talking specifically about Python dicts and sets, there is an 
additional restriction: both set elements and dict keys have to be 
hashable.

English being a little sloppy at times, sometimes we also talk about the 
*values of a set*, but here "value" is being used as a synonym for 
"element" and not as a technical term for the thing which mappings 
associate with a key.



-- 
Steven



More information about the Python-list mailing list