liveBook · Manning

Hashable

As explained in Python Workout: 50 ten-minute exercises

A hashable type has values with stable hash values and can therefore be used for dictionary keys or set elements.

A hashable type supports the hash function, so its values can serve as dictionary keys. Hashing helps keep keys unique and makes lookup fast rather than requiring a sequential search. section 4.1

Why hashing speeds lookup

Searching a string, list, or tuple may examine values one by one, taking O(n) time. A dictionary uses a hash-based location, analogous to sending visitors directly to an office by the first letter of a surname, giving approximately O(1) lookup. section 4.1

Keep these dictionary rules in mind:

  • Keys must be hashable, such as numbers or strings. section 4.23
  • Values can contain anything, including another dictionary. section 4.23
  • Keys are unique, and they can be iterated with loops or comprehensions. section 4.23

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines hashability through hash(), contrasts O(n) sequence search with O(1) dictionary lookup, and only raises collision handling.
Examples
Uses an office-directory analogy and contrasts strings, lists, and tuples with dictionary lookup.
What you can do after
Understand which values can serve as dictionary keys and why hashing makes lookup efficient.
How it connects
Connects hashable types with dictionaries, sets, keys, values, uniqueness, and lookup complexity.

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: 50 ten-minute exercises. The section links point to the parts of the book it draws on.