Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why can't I use a list as a dictionary key or set member?

Quick answer

Python requires dictionary keys and set members to be hashable. Lists are mutable containers and are not hashable, so using one in either place raises a TypeError. Use a tuple of hashable values when you need a compound key or set member. A tuple containing a list is not a workaround: every element in the tuple must be hashable too. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I tried using a list as a dictionary key and Python says it is unhashable. Why does that happen, and what should I use instead?

Geez

Dictionary keys and set members must be hashable. A list is a mutable container, and lists are not hashable, so Python rejects it in either role. [S1]

For example, this fails:

key = ["north", "shop"]
prices = {key: 12.50}  # TypeError: unhashable type: 'list'

If the two values represent one compound key and each value is hashable, use a tuple:

key = ("north", "shop")
prices = {key: 12.50}  # Works

locations = {("north", "shop")}  # Also works as a set member

The key idea is not simply “change square brackets to parentheses.” The tuple’s elements must also be hashable. [S1]

Follow-up question

I changed the outer list to a tuple, but my tuple contains a list and still fails. Is that expected?

Geez

Yes. A tuple is usable as a key or set member only when its elements are hashable. A list inside the tuple remains unhashable, so wrapping it does not fix the problem. [S1]

For instance, this still fails:

key = ("north", ["shop", "warehouse"])
prices = {key: 12.50}  # TypeError: unhashable type: 'list'

If those inner values are intended to be part of a fixed compound key, represent them as a tuple too:

key = ("north", ("shop", "warehouse"))
prices = {key: 12.50}  # Works, if all elements are hashable

Choose that representation only if it matches what the data means. A tuple is a suitable replacement for the key’s structure; it does not make mutable contents hashable.

Follow-up question

How can I choose a replacement without changing what my program means? I use the list to group a region and a type.

Geez

First decide what should identify one entry. If the region and type together identify it, use a tuple with those two values, provided both are hashable. If you need the values in a list elsewhere, keep that list for ordinary data and build a separate tuple for the key.

Worked example, assuming region and kind are strings:

region = "north"
kind = "shop"
key = (region, kind)
counts = {key: 3}

# Look up the same compound key:
print(counts[("north", "shop")])  # 3

This preserves the two-part identity while using a hashable tuple. The example assumes the values are strings; other value types must be checked for hashability before being used in the tuple. [S1] The source establishes the hashability requirement and the tuple rule; it does not determine which fields should define your application’s key. That choice depends on your data model.

Follow-up question

Does the same rule apply to sets, and what should I check when I see this error?

Geez

Yes. Set members, like dictionary keys, must be hashable, so a list cannot be added to a set. [S1] When you see an unhashable-type error, check the value being inserted and, if it is a tuple, check each element inside it.

For example:

items = {("north", "shop")}  # Works
items.add(["south", "shop"])  # TypeError: unhashable type: 'list'

Use a tuple of hashable values when the values together should act as one key or member. If the data must remain a list, do not use that list itself as the key or member; choose a separate representation that reflects the identity you need.

A practical checklist

  • Identify the exact value being used as the dictionary key or set member.

  • If it is a list, choose a hashable representation instead of inserting the list.

  • For a compound value, consider a tuple only when every element is hashable.

  • Inspect nested tuple elements; a list inside a tuple is still unhashable.

  • Confirm that the replacement representation matches what should identify the entry.

A few more useful answers

Can I use a tuple as a dictionary key every time?

No. A tuple is hashable only if all of its elements are hashable. A tuple containing a list is not a valid dictionary key. [S1]

Can a list be a member of a set?

No. Lists are mutable and unhashable, while set members must be hashable. Use a suitable tuple if its elements are all hashable. [S1]

Does changing a list to a tuple always preserve my program’s meaning?

Not necessarily. Use a tuple when the values should form one compound key or member; decide which values define that identity based on your data.

Sources

References used for this guide.

  1. [S1] Python Glossary: hashable
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work