Test Test - 5 months ago 7
Python Question

finding values under list python

Below is an example of my JSON file (which is pretty huge). I need to append multiple things. For

key2
,
key3
I have appended easily since it a pretty easy thing, but when I am trying to append
value1
and
value2
from
key1
. it gives me
TypeError: list indices must be integers, not str
.

[
{
"key1": [
{
"value1": int,
"value2": "string",
"value3": "string",
"value4": "string",
"value5": "string",
"value6": int
},
],
"key2": some value,
"key3": some value,
},
],

Answer

Keep in mind that there is a list and INSIDE that list is a dictionary.... So can't access it directly. Just to show I'll do it stepwise...

y = d['key1'][0]

Now y is a dictionary, and this is where you were going wrong.

Now you can access value1 like

value1 = y['value1']
Comments