Python error when trying to get dict item [duplicate]
I have dict
in python3
that look like this
{
'id': '435633',
'mysection': [
{
'myitem':
{
'myitem' : 'testdata'
}
}
]
}
I am trying to print the value of myitem
like this
print(mydict['mysection']['myItem']['myitem'])
But I get the error...
TypeError: list indices must be integers or slices, not builtin_function_or_method
Where am I going wrong?
mydict['mysection']
is a list, not a dict
print(mydict['mysection'][0]['myitem']['myitem'])
output:
testdata
mydict['mysection']
is a list [{'myitem': {'myitem' : 'testdata'}}]
The first item mydict['mysection'][0]
is a dictionary with your myitem
in, so
print(mydict['mysection'][0]['myItem']['myitem'])
will work.
However, you maybe want to loop in case you end up with more than one item (or even no items)