0

I want to store two values from JSON_file in new dict like this : {[1,2,5]:[0], [1,2,4]:[2]}

my JSON-file looks like this :

{
    "index": [
        {
            "timestamp": "2018-04-17 17:56:25",
            "src": "src",
            "dst": [1,2,5],
            "value": [0],
            "datatype": "datatype"
        },
        {
            "timestamp": "2018-04-17 18:00:43",
            "src": "src",
            "dst": [1,2,4],
            "value": [2],
            "datatype": "datatype"
        }
   ]
}

I wrote this code:

with open(filename) as feedjson:
    json_data = json.load(feedjson)
    feedjson.close()

list_dev = {}
for i in json_data["index"]:
    key = i['value']
    value = i['dst']
    list_dev[key] = value
print(list_dev)

I get this error:

list_dev.update({key: value})
TypeError: unhashable type: 'list'

can someone help me to fix this problem please?

3
  • Possible duplicate of Python: TypeError: unhashable type: 'list' Commented Apr 17, 2018 at 22:39
  • i saw this solution before. It doesn't help me to fix my problem. Thanks Commented Apr 17, 2018 at 22:42
  • Well the error you're having is easily fixable by converting the value variable into something immutable. This is the answer on both, original question and yours. Commented Apr 17, 2018 at 22:45

2 Answers 2

1

This is just for understanding purposes:

Dictionary keys should be immutable as explained here. In the question, [1,2,5] is a list which are mutable(contents can be modified with methods like append,pop, push) data types. So, the only way to use the entire contents of a list as a dictionary key(highly unusual) is to convert it to an immutable data type such as a tuple or string:

new_dict = {}              #initialize empty dictionary

dst = t['index'][0]['dst']      #[1,2,5]
value = t['index'][0]['value']  #[0]

new_dict[tuple(dst)] = value  #new_dict key "dst" as tuple

print(new_dict)     
--->{(1, 2, 5): [0]}

new_dict[str(dst)] = value  #new_dict key "dst" as string

print(new_dict)      
---->{'[1, 2, 5]': [0]}
Sign up to request clarification or add additional context in comments.

Comments

0

value is a list -> [1] or [2] or whatever, list is mutable object so you can't hash it

you can use the element in the list like key=i['value'][0] or convert the list to tuple like key=tuple(i['value']) both objects are immutable thus can be hashed and used as a key

by the way with provide context manager so you don't need to close the file using feedjson.close(), with will do it for you

5 Comments

Thank your for the answer. key=i['value'][0] i cant use this because i have a long JSON file.When I convert list to tuple key=tuple(i['value']) i get this error: TypeError: 'int' object is not iterable
@AhmyOhlin what are the different types of data that can appear under value field?
Value can be just integers
I fixed it by converting value to data of type "string" value = str(i['value']) key = str(i['dst']). Modify your answer from tuple to string please. Thanks for the help
Your example shows Value being a list, not an integer. The immutable version of a list is a tuple. I see nothing wrong with his answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.