hash
Calculate the hash of an object, the number dictionaries and sets use to look up keys.
Python 3.13
hash(object)Parameters
- objectAny
- A hashable object.
Return value
An int; raises TypeError for an unhashable object.
Examples
Equal numbers, equal hash
Python 3.13
print(hash(42), hash(1.0), hash(True))42 1 1
A list is not hashable
Python 3.13
try:
hash([1, 2])
except TypeError as error:
print(error)unhashable type: 'list'
Details
Equal objects always have the same hash, so 1, 1.0 and True are the same dictionary key. The hash of strings changes between program runs, so do not save it to a file. Mutable collections, such as lists, dictionaries and sets, are not hashable.
