set operations on dictionaries?

aba cus cus_aba at yahoo.com
Mon Oct 16 11:01:13 EDT 2006


A suggestion:

In effect, sets are dictionaries without values. (Even thought their implementation is different)
My suggestion would be to generalise set operations (such as union, intersection, etc.) to work for dictionaries just as they work on sets.

>>> Set([1,2,3]) - Set([3,4])
Set([1, 2])

>>> {1: 10, 2: 20, 3: 30} - {3: 30, 4: 40}

should give
{1: 10, 2:20}

instead of
"TypeError: unsupported operand type(s) for -: 'dict' and 'dict'"

Similarly, union ( | ) and intersetion ( & ) should also be very useful for dictionaries.

It should be quite obvious what should happen when the values stored for the relevant keys are identical. It is less obvious (and needs some though) what should  happen when the keys are the same in the two operands,  but the values are different.

E.g. 

Should

{1: 10, 2: 20} | {2: 25, 3:30}

give an error message (because the values for the key 2 don't match), or should this value conflict be somehow resolved automatically?
Similarly, should intersection and differenc eliminate keys only when the keys match, or not.

I think this would be useful. Would this be worth implementing? Would it be difficult to implement?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061016/a2e15469/attachment.html>


More information about the Python-list mailing list