NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior0 views

What is the time complexity of searching a Python dictionary (`dict`), and what can be used as a dictionary key?

Practice

Asked at

AI answer

On average, a lookup by key in a dict takes O(1): Python uses the key’s hash and a hash table. In the worst case, for example when there are many collisions, a lookup can take O(n). A key must be hashable: it must have a stable hash and correct equality comparisons. That’s why immutable types are usually suitable (str, int, a tuple of hashable elements, frozenset), but immutability alone does not guarantee hashability. Lists and ordinary sets cannot be used as keys.