This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: symmetric difference operation applicable to more than two sets
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Amit.Saha, docs@python, ezio.melotti, georg.brandl, iritkatriel, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2013-04-27 11:47 by Amit.Saha, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg187899 - (view) Author: Amit Saha (Amit.Saha) * Date: 2013-04-27 11:47
The description of the symmetric difference operation implies that it cannot be applied to more than two sets (http://docs.python.org/3/library/stdtypes.html#set.symmetric_difference).

However, this is certainly possible:

>>> s={1,2}
>>> t={2,3}
>>> u={3,4}
>>> s^t^u
{1, 4}
>>> s.symmetric_difference(t).symmetric_difference(u)
{1, 4}

I am not sure how much of a "semantic" sense that makes, given that symmetric difference is by definition for two sets. (http://en.wikipedia.org/wiki/Symmetric_difference).

So, either the operator should be fixed to allow only two sets or the description be updated.
msg187900 - (view) Author: Amit Saha (Amit.Saha) * Date: 2013-04-27 11:49
On some more thought, perhaps the description should be updated. Since s^t^u is effectively (s^t)^u and hence the implementation does not violate the definition.
msg187901 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2013-04-27 12:07
Can you suggest a change?  I don't see a problem here; giving multiple operators for the other operations does not imply that they are not treated as left-associative.
msg187902 - (view) Author: Amit Saha (Amit.Saha) * Date: 2013-04-27 12:16
I think the only change I am suggesting is the description of the ^ operator to be something like this:

set ^ other ^ ..
Return a new set with elements from the sets which are not present in more than one set

I do understand that this is not really what the operator and the corresponding operation symmetric_difference() allows semantically. But it does make it consistent with the description of operators such as the | or &, but then their operation allows multiple sets semantically.

Hmm may be it is fine as it is..
msg188328 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-05-03 20:44
Sets have methods that do not have operators (such as len, isdisjoint),
operators that do not have non-special methods (such as in, <), and method-operator pairs that do the same thing (such as (union, |), (symmetric_difference, ^)). For the pairs, it gives the method signature and the *equivalent* operator expression. Since .union takes multiple 'other' args, the equivalent operator expression does too. Since .symmetric_difference only takes one 'other' arg, so does the expression.

A coherent proposal would change the method code and doc to the following:

symmetric_difference(other, ...)
set ^ other ^ ...
    Return a new set with elements in an odd number of the sets.

s={1,2, 5}
t={2,3, 5}
u={3,4, 5}
print(s^t^u)
>>> 
{1, 4, 5}

I believe the proposal was once considered, and rejected. An argument for is that the effect of chained symmetric differences is not obvious, as evidenced by Amit's mistaken characterization. I had to think a bit before I was sure of the answer. An argument against is that what one actually gets is seldom wanted, so that allowing more than two inputs to the method would have little benefit. 

What might be done is to document the symmetric different of multiple sets with a parenthetical comment such as

"(The symmetric difference of multiple sets, a ^ b ^ c ^ ..., is a new set with elements appearing in an odd number of input sets.)"

This would let people know what to expect from such expressions, in a situation where the effect is less obvious than usual.
msg188376 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-05-04 16:44
> Return a new set with elements in an odd number of the sets.

This wording is not really clear to me.

IMHO the documentation is fine as is.  The evaluation order works as usual, and, since the symmetric difference is an associative (and commutative) operation, the order doesn't even matter.
msg188415 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-05-04 22:12
If you take the union/intersection/symmetric difference of n sets, the result is a set with all items that appears in one/all/an odd number of the n sets. The union and intersection methods actually accept n inputs, because the result is obvious, useful, and can be obtained faster that with n-1 binary operations. The symmetric_difference method does not, I presume because the result in not obvious (but that cuts both ways), not known to be useful, and perhaps would not be much faster than than n-1 binary operations.
msg381270 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-17 18:32
I agree that the doc is fine as it is.  If there will be no objections/suggestions in the next couple of weeks I will close this issue.
History
Date User Action Args
2022-04-11 14:57:45adminsetgithub: 62054
2020-11-18 00:45:04rhettingersetstatus: pending -> closed
resolution: rejected
stage: patch review -> resolved
2020-11-17 18:32:12iritkatrielsetstatus: open -> pending
nosy: + iritkatriel
messages: + msg381270

2013-05-04 22:12:48terry.reedysetmessages: + msg188415
2013-05-04 16:44:11ezio.melottisettype: behavior -> enhancement

messages: + msg188376
nosy: + ezio.melotti
2013-05-03 20:44:10terry.reedysetversions: + Python 2.7, Python 3.4
nosy: + rhettinger, terry.reedy

messages: + msg188328

stage: patch review
2013-04-27 12:16:31Amit.Sahasetmessages: + msg187902
2013-04-27 12:07:18georg.brandlsetnosy: + georg.brandl
messages: + msg187901
2013-04-27 11:49:10Amit.Sahasetmessages: + msg187900
2013-04-27 11:47:20Amit.Sahacreate