Home>

Please write your question in detail here.
(Example) I am making a ●● system with PHP (CakePHP).
■■ The following error message occurred while implementing the function.

Error message
Corresponding source code
Source code
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    It's a Japanese problem rather than a Python problem.

    "Lists, dictionaries, sets cannot be keys to other dictionaries"

    this is,
    "Lists can't be dictionary keys"
    "A set cannot be a key to a dictionary"
    "Dictionaries cannot be keys to (other) dictionaries"
    Is one sentence. Do you understand this?

  • Answer # 2

    Simply put, list, dict, and set cannot be used as dict keys.

    lst = [1, 2, 3]
    dct = {"a": 1, "b": 2}
    sets = {1, 2, 3}
    dct2 = {lst: 1}
    # TypeError: unhashable type:'list'
    dct2 = {dct: 1}
    # TypeError: unhashable type:'dict'
    dct2 = {sets: 1}
    # TypeError: unhashable type:'set'

    The reason why these classes cannot be used is that the contents cannot be uniquely determined and hashed because the contents can be freely changed (mutable) by adding and deleting elements after creating the object.

    unhashable type= =Types that cannot be hashed

  • Answer # 3

    It will be a supplement to otn.
    I happened to have "Introduction to python3" at hand, so I read it after a long time. To quote "3.7 Larger data structures", "The limitation is on the data type itself. For example, a dictionary key must be immutable, so lists, dictionaries, and sets can be keys for other dictionaries. No, but taples can be the key. "
    So I tried various experiments (with ipython). I hope this will deepen your understanding.
    In [6]: your_dict_on_tuple = {the_tuple:'on_tiple'} doesn't cause an error because it's OK.

    In [1]: the_list = [1,'a']
    In [2]: the_tuple = 1,'a'
    In [3]: the_dict = {1: '1','a':'a'}
    In [4]: ​​your_dict_on_list = {the_list: "on_list"}
    -------------------------------------------------- -------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-9-5c394629b423>in<module>
    ---->1 your_dict_on_list = {the_list: "on_list"}
    TypeError: unhashable type:'list'
    In [5]: your_dict_on_dict = {the_dict:'on_dict'}
    -------------------------------------------------- -------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-10-4295882fd0d0>in<module>
    ---->1 your_dict_on_dict = {the_dict:'on_dict'}
    TypeError: unhashable type:'dict'
    In [6]: your_dict_on_tuple = {the_tuple:'on_tiple'}
    In [7]: