'Sets can´t have sets as elements? [duplicate]
when i write in my code a set with a set as an element, when I run it, it returns "TypeError: unhashable type: 'set'"
Example 1:
{{"why_this_doesn´t_work?"}}
TypeError: unhashable type: 'set'
Example 2:
A={{"a","b"},{"c"}}
print(A)
TypeError: unhashable type: 'set'
why does this happens and what does hashable means?
Solution 1:[1]
Sets can only contain hashable items. It can be the built-in immutable python objects or a custom object with a defined __hash__ function.
The question that raises also, why sets force to contain hashable items and not lists ?
Sets have the property of containing unique elements. Adding an element to a set requires comparing it to the existing set elements.
This operation of comparison is possible only if the objects are hashable, and they hash is unique during the lifetime of the object, so they are considered as a unique identifier to perform the __eq__ operation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Khalil Gorsan mestiri |
