When using a dictionary in Python, the following is impossible:
d = {}
d[[1,2,3]] = 4
since 'list' is an unhashable type
. However, the id
function in Python returns an integer for an object that is guaranteed to be unique for the object's lifetime.
Why doesn't Python use id
to hash a dictionary? Are there drawbacks?
The reason is right here (Why must dictionary keys be immutable)
In Python dictionaries keys are compared using
==
, and the equality operator with lists does an item-by-item equality check so two different lists with the same elements compare equal and they must behave as the same key in a dictionary.If you need to keep a dictionary or set of lists by identity instead of equality you can just wrap the list in a user-defined object or, depending on the context, may be you can use a dictionary where elements are stored/retrieve by using
id
explicitly.Note however that keeping the
id
of an object stored doesn't imply the object will remain alive, that there is no way for going fromid
to object and thatid
may be reused over time for objects that have been garbage collected. A solution is to useinstead of
It is a requirement that if
a == b
, thenhash(a) == hash(b)
. Using theid
can break this, because the ID will not change if you mutate the list. Then you might have two lists that have equal contents, but have different hashes.Another way to look at it is, yes, you could do it, but it would mean that you could not retrieve the dict value with another list with the same contents. You could only retrieve it by using the exact same list object as the key.