Sobes.tech
Junior — Middle

Can you explain what data types can be hashed?

sobes.tech AI

Answer from AI

In Python, "hashable" data types refer to objects that have an immutable state and support the __hash__() method. This means their value does not change after creation, and they can be used as keys in dictionaries or elements of sets.

Typical hashable types include:

  • Numbers (int, float, complex)
  • Strings (str)
  • Tuples (tuple), if all elements inside are also hashable
  • Boolean values (bool)

Unhashable types are mutable objects, such as lists (list), dictionaries (dict), sets (set), because their contents can change, which would break hash integrity.

Example:

hash(42)          # works
hash("hello")    # works
hash((1, 2, 3))   # works
hash([1, 2, 3])   # TypeError: unhashable type: 'list'