I would like to understand what elements can be tested for set membership in Python. In general, setting up membership testing works like list membership testing in Python.
>>> 1 in {1,2,3} True >>> 0 in {1,2,3} False >>>
However, sets differ from lists in that they cannot contain unpackable objects, such as nested sets.
The list is all right:
>>> [1,2,{1,2}] [1, 2, {1, 2}] >>>
Install does not work because it does not shake:
>>> {1,2,{1,2}} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'set' >>>
Now, even if sets cannot be members of other sets, we can use them in membership tests. Such a check does not lead to an error.
>>> {1} in {1,2,3} False >>> {1,2} in {1,2,3} False >>> set() in {1,2,3} False >>>
However, if I try to run the same test where the item being tested is dict , I get an error message that suggests that the item being tested cannot be disabled.
>>> {'a':1} in {1,2} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict' >>> {} in {1,2} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict' >>>
This may not be the whole story, because set can be tested for membership in another set, even if it does not shake itself, giving a result, not an error.
So the question is: What does an element do for a member set test in Python?