I test a piece of code as follows:
>>>s = set(([1, (2, 3)])) #OK # 1
>>>s
{(1, (2, 3))} # put a tuple as set element
>>>s = set([(1, [2, 3])]) #error # 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>s = set([[1, 2]]) #error # 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
When I put a tuple as a set element (in 1), it works. But if I put a tuple containing a list as an element (in 2) or just put a list (in 3) in a set element, it went wrong.
I know coding like this: list = [1,2]; s = set([list])
is not allowed because we can change list
later. But I just don't understand, since [1,2]
itself is immutable and won't change, why I can't code s = set([[1,2]])
.