Throw TypeError: unhashable type: 'list' in Python 3.8.2

Dung Do Tien Aug 22 2021 135

Hello, I have a small problem. I have an array to management range age of the human. You can see as below:

 age_range=[
    {
    "name":"Little Boy",[0,4]:"Little boy age",
    "name":"Children",[5,15]:"Children age",
    "name":"Younger",[16,30]:"Younger age"
    }
]
print(age_range)

But when I trying to print out my array I get an error throw TypeError: unhashable type: 'list'.

 Traceback (most recent call last):
  File "main.py", line 2, in <module>
    {
TypeError: unhashable type: 'list'

I don't understand what is unhashable to an array?

Anyone can explain it to me?

Have 1 answer(s) found.
  • A

    An Pham Truong Aug 22 2021

    This error raises because your object keys are an array, it's not a single value.

    So to use a range key value in an object, you can use tuple() method to help defined value. See an example below:

    age_range=[
        {
        "name":"Little Boy",tuple([0,4]):"Little boy age",
        "name":"Children",tuple([5,15]):"Children age",
        "name":"Younger",tuple([16,30]):"Younger age"
        }
    ]
    print(age_range)
    #OUTPUT
    [{'name': 'Younger', (0, 4): 'Little boy age', (5, 15): 'Children age', (16, 30): 'Younger age'}]

    I hope it is useful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close