python typing ai_generated true

TypeError: unhashable type: 'list'

ID: python/typeerror-unhashable-list

Also available as: JSON · Markdown
97%Fix Rate
98%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
310 active
311 active

Root Cause

Attempting to use a list as a dict key or set element.

generic

Workarounds

  1. 97% success Convert list to tuple before using as dict key or set element
    Use tuple(my_list) as the key instead

    Sources: https://docs.python.org/3/glossary.html#term-hashable

  2. 90% success Use frozenset() if order doesn't matter
    frozenset(my_list) is hashable and order-independent

Dead Ends

Common approaches that don't work:

  1. Wrapping the list in str() to make it hashable 70% fail

    Loses structure; str representation can collide for different lists

  2. Using json.dumps() as hash key 55% fail

    Fragile — key ordering matters, wastes memory

Error Chain

Frequently confused with: