Yes, you can use the set.difference_update() method (or the -= operator):
>>> s = {1, 2, 3, 4, 5}
>>> s.difference_update({1, 2, 3})
>>> s
{4, 5}
>>> s -= {4, 5}
>>> s
set()
Note that the non-operator version of difference_update() will accept any iterable as an argument. In contrast, its operator-based counterpart requires its argument to be a set.
python - How to discard multiple elements from a set? - Stack Overflow
Python remove set from set - Stack Overflow
Remove multiple elements from a list
Delete Diagonal elements from a tensor in Tensorflow ?
Videos
You need to call discard on individual items, not on the generator of items to be discarded:
for x in [x for x in a if len(x.split()) < 9]:
a.discard(x)
Be mindful that you can't discard items while iterating through the set, so this will not work:
for x in a:
if len(x.split()) < 9:
a.discard(x)
Although this is beyond your question, I'd like to add that there are better ways to do what you want through set comprehension or set subtraction as suggested in another answer and comments.
You are using the wrong method to remove elements from the set. discard removes an element from the set only if it exists. You want to remove elements based on a condition, so you need to use a different approach. Here's a corrected version of the code:
a = {'ab', 'z x c v b n m k l j h g f f d s a a', 'q w e r t y u i o p'}
a = {x for x in a if len(x.split()) >= 9}
print(a)
This code creates a new set with only the elements that meet the condition, and then assigns it back to a. The desired output is achieved:
{'z x c v b n m k l j h g f f d s a a', 'q w e r t y u i o p'}
set1-set2
set1 = {0,1,2,3}
set2 = {2,3,4,5}
set1 - set2 # {0, 1}
set2 - set1 # {4, 5}
However, note that for whatever reason you can't "+" sets in python...
You already answered the question. It refers to sets of sets (actually sets containing frozensets).
The paragraph you are referring to begins with:
Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set.
which means that b in a.remove(b) can be a set, and then continues with:
To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value.
which means that if b is a set, a.remove(b) will scan a for a frozenset equivalent to b and remove it (or throw a KeyError if it doesn't exist).