liveBook · Manning

Hashable

As explained in Python Workout, Second Edition

A hashable object is a Python object with a stable hash value that can be used as a dictionary key.

A hashable object is a Python object that provides a hash value and can therefore serve as a dictionary key. Hashing lets a dictionary locate a key directly instead of scanning every item, making lookup effectively constant-time as the collection grows. section 5.1

When choosing dictionary keys, remember:

  • Use hashable values such as numbers or strings as keys. section 5.20
  • Keep values unrestricted: a value may be anything, including another dictionary. section 5.20
  • Expect keys to be unique; hashing supports efficient key searches. section 5.1 section 5.20

Choose the key, then store any value

Use a string or number for the dictionary key, then attach the data you need: a key such as "Smith" can identify a record whose value could itself be another dictionary. If several entries need the same key, the keys are not distinct. section 5.1 section 5.20

How Python Workout, Second Edition covers it

How deep it goes
Explains hash functions through lookup behavior and complexity, but stops short of implementation details.
Examples
Uses numbers and strings as keys and contrasts unrestricted dictionary values with unique keys.
What you can do after
Apply it by selecting hashable objects for dictionary keys while storing arbitrary values.
How it connects
Connects hash functions with dictionaries, keys, uniqueness, and constant-time lookup.

Also explained in

Compare how 3 books explain hashable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.