Hey does anybody know how I would go about getting the value of a key which is already inside another key like this:
a = {"fruit":[{"oranges":['10']},{"apples":['11']}]}
print(a.get("fruit"))
I can get the value of "fruit" but how would I get the value of "oranges". Thank you for any help in advance.
CodePudding user response:
Let's format your dictionary and clearly see what you have:
a = {
"fruit": [
{
"oranges": ['10']
},
{
"apples": ['11']
}
]
}
So, a.get('fruit')
gives you a list
, which elements can be accessed with indexes.
So a['fruit'][0]
gives you
{
"oranges": ['10']
}
and a['fruit'][1]
gives you
{
"apples": ['11']
}
So in order to get the value of oranges
you should go with:
a['fruit'][0]['oranges']
which will give you: ['10']
. ['10']
is a list of its own. If you want to get only the value, you can do:
a['fruit'][0]['oranges'][0]
CodePudding user response:
You just have to access the first element of the list inside fruits, and then access the key inside
print(a['fruit'][0]['oranges']