StudyGraph
Computer Science Why?

Why Can't a List Be a Dictionary Key?

Why can't you use a Python list as a dictionary key?

Lesson notes

Key points

Definitions

Hashable
An object whose hash never changes in its lifetime and that can be compared for equality.

Takeaway: So keys must be hashable: use immutable types like tuples or strings.

Quick check: What property must a dictionary key's hash have over time?
  1. It must grow
  2. It must stay constant
  3. It must be prime

A changing hash would send lookups to the wrong bucket.

Hash picks bucketMutabilityLost entry

Lessons like this, for your syllabus.

Short narrated study videos written for your course, level and exam. Free to start.

Get lessons for your own syllabus