On 10/5/2010 3:39 AM, Steven D'Aprano wrote:
Sets aren't an alternative to dictionaries. They have a completely different purpose.
A dict/mapping is a specialized set -- a set of ordered pairs in which each first member (the 'key') only appears once as a first member. The set union of two mappings may not be a mapping, which is why dicts have a specialized .update method instead of .union.
It so happens that sets and dicts *in Python* happen to share some implementation details, and they happen to share similar syntax, but don't be fooled by these incidental details. They could be implemented differently. Dicts are used when you need a 1:1 mapping between a key and a value:
The mapping does not have to be 1:1, which means that each value also appears only once, so that the mapping can be inverted. Values can appear more than once, which is to say, many keys can map to one value. so the mapping is many:1. Collections (sets or lists) are used as values to implement 1:many mappings.
-- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list