List unhashable, but tuple hashable?

前端 未结 6 869
渐次进展
渐次进展 2020-12-08 10:15

In How to hash lists? I was told that I should convert to a tuple first, e.g. [1,2,3,4,5] to (1,2,3,4,5).

So the first cannot be hashed, bu

6条回答
  •  佛祖请我去吃肉
    2020-12-08 10:29

    The answers are good. The reason is the mutability. If we could use list in dicts as keys; (or any mutable object) then we would be able to change the key by mutating that key (either accidentally or intentionally). This would cause change in the hash value of the key in dictionary due to which we would not be able to retrace the value from that data structure by that key. Hash values and Hash tables are used to map the large data with ease by mapping them to indices which stores the real value entries.

    Read more about them here:-

    Hash Tables & Hash Functions & Assosiative Arrays

提交回复
热议问题