According to my interpretation of the Python 2.7.2 documentation for the built-in types of 5.7 Set Types , it should be possible to remove the elements set A from set B by passing A to set.remove(elem) or set.discard(elem)
From the documentation for 2.7.2:
Note that the elem argument to the __contains__() , remove() and discard() methods can be a set.
I interpret this as meaning that I can pass set to remove(elem) or discard(elem) and all of these elements will be removed from the target set. I would use this to do something weird, like removing all vowels from a string or removing all common words from word history . Here's the test code:
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [M... Type "help", "copyright", "credits" or "license" >>> a = set(range(10)) >>> b = set(range(5,10)) >>> a set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b set([8, 9, 5, 6, 7]) >>> a.remove(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: set([8, 9, 5, 6, 7]) >>> a.discard(b) >>> a set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>>
What I expect to return:
>>> a set([0, 1, 2, 3, 4])
I know that I can accomplish this with a.difference(b) , which returns a new set; or with a set.difference_update(other) ; or with operator statements a -= b that change the set in place.
So is this a mistake in the documentation? Can set.remove(elem) not actually accept the set as an argument? Or does the documentation relate to kits? Given that difference_update is fulfilling my interpretation, I assume the latter is the case.
Is this not clear enough?
EDIT After 3 years of additional (some professional) python work and recently addressing this issue, I understand that what I'm actually trying to do can be achieved with:
>>> c = a.difference(b) set([0,1,2,3,4])
because of which I initially tried to get.